C++函数调用运算符重载

#include<iostream>
#include<string>
using namespace std;
class MyPrint
{
public:
    void operator()(string text) {
        cout << text << endl;
    }
};
class MyAdd {
public:
    int operator()(int n1, int n2) {
        return n1 + n2;
    }
};

int main() {
    MyPrint mp;
    mp("hello c++");//仿函数
    cout << MyAdd()(100, 100) << endl;
    system("pause");
    return 0;
}

 

posted on 2020-03-13 19:56  ~明月几时有  阅读(188)  评论(0)    收藏  举报