Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented May 15, 2017

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
You can’t perform that action at this time.