Created
May 28, 2022 16:49
-
-
Save SealtielFreak/46fa27ea902ecb9d661553c73da558f6 to your computer and use it in GitHub Desktop.
Functor 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
template<typename R, typename ...Args> | |
class Functor { | |
protected: | |
virtual R call(Args ...args) = 0; | |
public: | |
R operator()(Args ...args) { | |
return call(args...); | |
} | |
}; | |
class Foo : public Functor<int, int> { | |
int n = 0; | |
int call(int i) { | |
n += i; | |
return n; | |
} | |
}; | |
/* | |
// Example usage: | |
int main() { | |
Foo foo; | |
foo(1); | |
foo(3); | |
foo(1); | |
int i = foo(0); // output: 5 | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment