#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("helloworld");//由于使用起来非常类似函数调用,因此称为仿函数
Myprint02("hello world");
}
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test02()
{
MyAdd myadd;
cout << myadd(2, 3) << endl;
//匿名函数对象 类名+() 特点:当前行执行完立即释放
cout << MyAdd()(100, 100) << endl;//后一个()是使用它重载的运算符
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}