Created
June 7, 2022 10:47
-
-
Save aviralgoel/2596c5af5fc0058074c35c51fbcc98a6 to your computer and use it in GitHub Desktop.
an example of functors in cpp
This file contains 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> | |
using namespace std; | |
class myIncreasingFunctor { | |
int increaseBy; | |
public: | |
myIncreasingFunctor(int _value) { | |
increaseBy = _value; | |
} | |
int operator () (int _value) { | |
return _value + increaseBy; | |
} | |
}; | |
int main() | |
{ | |
myIncreasingFunctor IncreaseBy5(5); | |
cout << IncreaseBy5(10); // output 15 | |
cout << IncreaseBy5(20); // output 25; | |
myIncreasingFunctor IncreaseBy10(10); | |
cout << IncreaseBy10(10); // output 20 | |
cout << IncreaseBy10(20); // output 30; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment