c ++ function calls the operator overloading

Use function calls () can be overloaded.

Since the use of a very heavy load calling functions, so called functor.

Functor no fixed wording, very flexible.

#include<iostream>
using namespace std;

class MyPrint {
public:
    void operator()(string test) {
        cout << test << endl;
    }
};
class MyAdd {
public:
    int operator()(int num1, int num2) {
        return num1 + num2;
    }
};
void test() {
    MyPrint myPrint;
    MyAdd myAdd;
    myPrint("hello world");
    int res = myAdd(1, 2 ); 
    COUT << RES << endl;
     // anonymous function object 
   COUT << myAdd () (. 1, 2) << endl; 
} 


int main () { 
    Test (); 
    System ( " PAUSE " );
     return  0 ; 
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/12100413.html