#include#include #include using namespace std; //函数对象(仿函数) //可以像普通函数那样有参数有返回值 //可以有自己的状态 //可以作为参数传递 class Person { public://函数对象这个名称只能使用operator int operator()(int v1,int v2){ return v1 + v2; } }; void test01() { Person person; cout << person(10, 10) << endl; } class Person1 { public://函数对象这个名称只能使用operator Person1() { this->count = 0; } //可以让他有一个内部状态变成内部属性 //重载括号 void operator()(string test) { cout << test << endl; this->count++; } int count = 0; }; void doPrint(Person1 person3,string test) { person3(test);//让这个函数可以直接打印test } void test02() { Person1 person1; person1("hello"); person1("hello"); person1("hello"); person1("hello"); cout << person1.count << endl; } //函数对象作为参数传递 void test03() { Person1 person12; doPrint(person12,"hello"); } int main() { //test02(); test03(); system("pause"); return 0; };



