Skip to content

Instantly share code, notes, and snippets.

@aviralgoel
Created June 7, 2022 10:47
Show Gist options
  • Save aviralgoel/2596c5af5fc0058074c35c51fbcc98a6 to your computer and use it in GitHub Desktop.
Save aviralgoel/2596c5af5fc0058074c35c51fbcc98a6 to your computer and use it in GitHub Desktop.
an example of functors in cpp
#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