One of the definitions of FunctorObject''''s is... In the CeePlusPlus StandardTemplateLibrary, a "FunctorObject" is an instance of a class that implements "operator()." As such, its usage is syntactically indistinguishable from a FunctionPointer -- a property that lets FunctorObject''''s and FunctionPointer''''s to be used interchangeably in template-based GenericAlgorithms. See: FunctorObject ----- Benefits of using objects instead of FunctionPointer''''s: * The compiler can inline methods with FunctorObject''''s. * The object can have local state. ''(Like, it can "multiply by 6," where 6 is a value determined at run time.)'' ----- "What are StlFunctionObjects?" / how to create and use them... * "Defining a Function Object" -- http://www.inquiry.com/techtips/cpp_pro/10min/10min0100.asp * See Chapter 5, '''"STL Algorithms"''', of ''Thinking in C++:'' -- http://www.camtp.uni-mb.si/books/Thinking-in-C++/Chapter05.html * ''...and'' http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/Thinking_in_C++/tic0235.html ----- ''[discussion...]'' ----- If C++ had GarbageCollection, a FunctionObject could be used to give C++ true higher-order functions (though with an ugly syntax). One could create and return a FunctionObject (or a reference/pointer to same) in a HigherOrderFunction, use it as necessary, and let the GC dispose of it. Without GC, though; this use becomes problematic as the user must then manually free the function object. Given the typically usage patterns of function objects created in such fashion (they often get passed around, or stuffed in a data structure, and die well after they are created), manual reclaimation of these is a RoyalPain (i.e. error-prone and likely to leak memlry). Workarounds: * Use a SmartPointer of some sort. A poor-man's solution (see ReferenceCounting) * Copy them by value. Works if the function object has a small amount of state; but for larger function objects this becomes expensive. * Use only statically-alocated function objects. This prevents true higher order functions (which can return an infinte number of different functions, depending on the arguments to the higher-order function) * Use the methods of '''FC++''' (see FunctoidsInCpp) where the internals do contain reference counting. ----- CategoryObjectFunctionalPatterns CategoryCpp CategoryFunctionalProgramming