Re: What is a functor in C++?
W. J. La Cholter wrote:
[...]
A functor is an object that can behave like a function. It does so by
overloading operator () (i.e., function call). It should generally
implement operator () as a const member function, i.e., doesn't change
the object or is stateless. It should implement cheap copy construction
and assignment because temporaries are often created and should be
optimized.
This is true for functors which are to be used by the STL (and
it often makes life unnecessarily difficult). One of the major
motivations of using a functor instead of a simple pointer to
function, however, is in order for it to have state, in addition
to just being a function. (In the case of the STL, this doesn't
create problems if the state is constant and cheap to copy.
Otherwise, some sort of indirection to the state is necessary.)
And of course, one of the major reasons for using functors
outside of the STL is polymorphism, which doesn't allow copying
either.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]