A functor is an object that behaves as a function, thanks to the overloading of operator (). This allows the encapsulated function to have a state, as opposed to mere C function pointers: this state allows to first setup and parametrize the functor (thanks to its constructor), then to use it as "a lambda function".
- It is especially useful when one has to specify a function A as a parameter for another function B. If B is set so that its argument shall be a functor, then it can take indifferently a simple function or a functor as parameter. The reciprocal would not be true.
Functors are great when one wants to make use of parametrized functions: thanks to the functor's constructor, one can generate numerous different functions at runtime.
( int myAdd ) could be constructed as:
Functor * myAdd = new addFunctor( 5 )
: myAdd is a dynamically created function.
- See also:
- Ceylan::Maths::IntToIntFunctor for a complete example.
- Each child class should define following operator: virtual 'returned type' operator() ('parameter') throw()