- 函数调用运算符()也可以重载
- 由于重载后使用的方法非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
#include <iostream>
using namespace std;
#include <string>
class Myprint
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
void Myprint02(string test)
{
cout << test << endl;
}
void test01()
{
Myprint myprint;
myprint("hello");
Myprint02("world");
}
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test02()
{
MyAdd myadd;
int ret = myadd(100, 100);
cout << ret << endl;
cout << MyAdd()(100, 100) << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}