Although function pointers are widely used to implement function callbacks, C + + provides an important way to implement callback functions, which are function objects. A function object (also called a "operator") is an ordinary class object that overloads the () operator. Therefore, the function object is syntactically similar to the normal function behavior.
There are several advantages to using a function object instead of a function pointer, first, because the object can be modified internally without altering the external interface, so the design is more flexible and more resilient. The function object also has a data member that stores the result of the previous call. You need to store the result of a previous call in a full or local static variable when using a normal function, but there are some flaws that we don't want to see in the whole or local static variable.
Second, the compiler can implement inline calls in the function object, which further enhances performance. This is almost impossible in a function pointer.
The following examples illustrate how to define and use function objects. First, declare a common class and overload the "()" Operator:
Class Negate {public:int operator () (int n) {return-n;}};
In an overloaded action statement, remember that the first parenthesis is always empty, because it represents the overloaded operator name, and the second parenthesis is the argument list. In general, when overloading an operator, the number of arguments is fixed, and the overloaded "()" operator differs, and it can have any number of arguments.
Because the built-in operation in negate is unary (with only one operand), the overloaded "()" operator has only one parameter. The return type is the same as the parameter type-in this case, int. The function returns an integer that is reversed from the parameter symbol.
Working with Function objects
We now define a function called callback () to test the function object. Callback () has two parameters: one is an int and the other is a reference to the class negate. Callback () Neg the function object as a normal function name:
Filename:negate.h#include <iostream>using std::cout;void Callback (int n, Negate & neg) {int val = neg (n); Call the overloaded operator "()" Cout << Val;}
Code, note that neg is an object, not a function. The compiler adds statements
int val = neg (n);
Translates to
int val = neg.operator () (n);
Typically, function objects do not define constructors and destructors. Therefore, no problems occur during the creation and destruction process. As mentioned earlier, the compiler can inline overloaded operator code, thus avoiding run-time issues related to function calls.
To complete the example above, we use the main function main () to implement the callback () parameter passing:
#include "Negate.h" int main () {Callback (5, Negate ());//Output-5}
This example passes the integer 5 and a temporary negate object to callback (), and then the program outputs-5.
Template Function Object
As can be seen from the above example, its data type is limited to int, and universality is one of the advantages of function object, how to create a function object with generality? The method is to use a template, that is, to define the overloaded operator "()" as a class member template so that the function object applies to any data type: such as double,string or char:
#include <iostream>using namespace Std;class genericnegate{public:template <class t> t operator () (T T) const {return t;}}; int main () {genericnegate negate; cout<< negate (5.3333) <<endl; Double cout<< negate ("Hello") <<endl; String
It is quite difficult to achieve the above flexibility with a normal callback function.
function objects in the standard library
The C + + standard library defines several useful function objects that can be placed into the STL algorithm. For example, the sort () algorithm takes the decision object (predicate object) as its third parameter. The decision object is a templated function object that returns a Boolean result. You can pass greater<> or less<> to sort () to force the ascending or descending order of the sorting:
How to define a function object in C + +