函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定的写法
#include<iostream>
#include<string>
using namespace std;
class Myprint {
public:
//重载函数调用运算符
void operator()(string text) {
cout << text << endl;
}
int operator()(int a,int b){
return a + b;
}
};
void test01() {
Myprint m1;
m1("helloworld");
cout<<m1(100, 100)<<endl;
}
int main() {
test01();
return 0;
}

拓展匿名对象
匿名对象就是Myprint()(100,100)
用()来代替了对象的名字,这个匿名对象当前执行完后会立刻被释放
最后
以上就是小巧鸭子最近收集整理的关于C++函数调用运算符重载+匿名对象的全部内容,更多相关C++函数调用运算符重载+匿名对象内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复