Overload class function call operator, which is often referred to as the object function object (function object), i.e., they are similar to the behavior of the object function, also called functor (functor), is actually overloaded "()" operator, such that class object like a function call as possible.
Note: a function object (functor) is a class, not a function. 2. A function object (functor) overrides "()" operator so that it can be called like a function.
Suppose that a class has an overloaded operator (), and overloaded operator () Gets a required parameter, we will call this class "single functor" (unary functor); the contrary, if the overloaded operator ( ) which requires the two parameters, the class will be referred to as "binary functor" (binary functor).
#include <iostream> #include <vector> #include <algorithm> using namespace std; class FuncObject01 { public: void operator()() { cout << "Hello WOrld!" << endl; } }; void FuncObject02() { cout << "Hello WOrld!" << endl; } void test01() { FuncObject01 fobj; fobj(); FuncObject02 (); COUT << " ----------------------- " << endl; } class FuncObject03 { public : int operator () ( int A, int B) { return A + B; } } ; int FuncObject04 ( int A, int B) { return A + B; } // function object may be the same as a normal function has a return value and parameters void Test02 () { FuncObject03 F obj; int= F obj RET ( 10, 20 is ); COUT << " RET = " << RET << endl; RET = FuncObject04 ( 10 , 30 ); COUT << " RET = " << RET << endl; COUT << " ----- ------------------- " << endl; } // function object beyond the ordinary functions of the function, the function call may have saved state // for example, we want to statistical functions the number of calls class FuncObject05 { public : FuncObject05 ():count(0) {} void operator () () { cout << " the Hello World! " << endl; COUNT ++ ; } int COUNT; }; // common frequency statistics call a function that requires a global variable int g_count = 0 ; void FuncObject06 () { COUT << " ! the Hello World " << endl; g_count ++ ; } void TEST03 () { FuncObject06 (); FuncObject06 (); COUT << " number of function calls: " << g_count << endl; // use the function object without the use of global variables FuncObject05 F obj; F obj (); F obj (); F obj (); COUT << " function call times: " < <fobj.count << endl; COUT << " --------------------------- " << endl; } // function object do parameter and return value class Print { public : Print ():count(0){} void operator()(const int& val) { cout << val << " "; count++; } int count; }; int num = 0; void print2(int val) { cout << val << " "; num++; } void test04() { vector<int> v; v.push_back(1); v.push_back(3); v.push_back(. 5 ); v.push_back ( test02();2 ); // iterate through the container elements for_each algorithm Print myPrint; // function object parameters and return values do myPrint = for_each (v.begin (), v.end (), myPrint); COUT << endl ; cout << " function call times: " << myprint.count << endl; for_each (v.begin (), v.end (), print2); cout << endl; cout << " function call times: " NUM << << endl; } int main () { Test01 (); TEST03 (); test04(); getchar(); return 0; }