Functors (function objects) are classes that are callable like functions and can hold state between calls. The below example shows an obj doubling in value with every function call:
Doubler obj(1);
obj(); // value = 2
obj(); // value = 4
Functors are implemented by overloading the function call operator.
struct Doubler {
int value;
Doubler(int v) : value(v) {}
void operator()() {
value *= 2;
}
};
Dan