Skip to content

Instantly share code, notes, and snippets.

@wangkuiyi
Created May 15, 2017 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangkuiyi/02452f8908d41b52a4ac8242c03d7c43 to your computer and use it in GitHub Desktop.
Save wangkuiyi/02452f8908d41b52a4ac8242c03d7c43 to your computer and use it in GitHub Desktop.
A simple example on C++ functor
#include <iostream>
class Image {
};
class blur {
public:
blur(double factor) : factor_(factor) {}
void operator()(Image & img) const {
std::cout << "Bluring image using factor=" << factor_ << "\n";
}
private:
double factor_;
};
class compress {
public:
compress(int width, int height) : width_(width), height_(height) {}
void operator()(Image & img) const {
std::cout << "Compressing image to new size (" << width_ << ", " << height_ << "\n";
}
private:
int width_, height_;
};
int main() {
Image img;
blur(1.0)(img);
compress(100, 200)(img);
}
@wangkuiyi
Copy link
Author

Build and run on my Mac

$ clang++ functor_example.cc -o functor_example  && ./functor_example
Bluring image using factor=1
Compressing image to new size (100, 200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment