Skip to content

Instantly share code, notes, and snippets.

@SealtielFreak
Created May 28, 2022 16:49
Show Gist options
  • Save SealtielFreak/46fa27ea902ecb9d661553c73da558f6 to your computer and use it in GitHub Desktop.
Save SealtielFreak/46fa27ea902ecb9d661553c73da558f6 to your computer and use it in GitHub Desktop.
Functor in CPP
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