热门

最新

红包

立Flag

投票

同城

我的

发布
xinzhilinger
心之凌儿
3 年前
truexinzhilinger

拿到一份小礼物,很开心

CSDN App 扫码分享
分享
2
3
打赏
  • 复制链接
  • 举报
下一条:
函数调用运算符重载函数调用运算符()也可以重载由于重载后使用的方式非常像函数的调用,因此称为仿函数仿函数没有固定写法,非常灵活 #include<iostream>#include<string>using namespace std;//函数调用运算符重载class MyPrint{public: //重载函数调用运算符 void operator()(string text) { cout << text << endl; } };class MyAdd{public: int operator()(int a, int b) { return a + b; }};void test(){ MyPrint myprint; myprint("hello world"); MyAdd myadd; cout << myadd(1, 2) << endl; //匿名函数对象——特点:当前行被执行完立即释放 cout << MyAdd()(100,100) << endl;}int main(void){ test(); system("pause"); return 0;}
立即登录