#include<iostream>
#include<string>
using namespace std;
class Myprint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
//仿函数很灵活,没有固定写法
//加法类
class Myadd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test01()
{
Myprint myprint;
myprint("重载了哦");//由于使用起来非常类似于函数调用,因此称为仿函数
}
void myPrint(string test1)
{
cout << test1 << endl;
}
void myAdd()
{
Myadd m1;
int ret=m1(1, 2);
cout << "ret= " << ret << endl;
//匿名函数对象,匿名使用完会立即释放
cout << "ret= "<<Myadd()(1, 2) << endl;
}
int main()
{
test01();
myPrint("张三");
myAdd();
}