函数调用运算符()也可以重载;
由于重载后的使用方式和函数调用非常像,因此成为仿函数;
仿函数没有固定写法,非常灵活;
例一:
class Person
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
void test01()
{
Person p1;
p1("74107410");
Person()("74107410");
}
例二
class student
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
void test02()
{
student s1;
cout << s1(10, 10) << endl;
cout << student()(10, 10) << endl;
}