Created
May 15, 2017 15:51
-
-
Save wangkuiyi/02452f8908d41b52a4ac8242c03d7c43 to your computer and use it in GitHub Desktop.
A simple example on C++ functor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Build and run on my Mac