4.9. Implementing a Function Object

In Section 3.6, we looked at the predefined function objects of the standard library. In this section, we look at how to implement our own function objects. A function object is a class that provides an overloaded instance of the function call operator.

When the compiler encounters what appears to be a function call, such as

lt( ival ); 

lt can be the name of a function, a pointer to function, or an object of a class that has provided an instance of the function call operator. If lt is a class object, the compiler internally transforms the statement as follows:

lt.operator()( ival ); // internal transformation 

The function call operator can take any number of parameters: none, one, two, and so on. It ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.