#include<iostream>
using namespace std;
#include<string.h>
//函数调用运算符重载
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test01() {
//重载的()操作符,也称为仿函数
MyPrint myprint;
myprint("hellow");
}
class MyAdd
{
public :
int operator()(int a, int b)
{
return a + b;
}
};
void test02() {
MyAdd myadd;
int red=myadd(120, 20);
cout << red << endl;
//匿名对象的调用
cout << "myadd()(100,100)=" << MyAdd()(100, 100) << endl;
}
int main(){
test01();
test02();
return 0;
}
总结:C++运算符的重载,函数调用()的重载是非常灵活的,根据实际情况重载运用。