黑马237 #include #include #include #include #include using namespace std; //函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值 class myClass { public: int operator()(int v1, int v2) { return v1 + v2; } }; //函数对象超出普通函数的概念,函数对象可以有自己的状态 class myPrint { public: myPrint() { this->count = 0; } void operator()(string test) { cout << test << endl; count++; } int count;//拥有自己的状态 }; void test01() { myClass add; cout << add(10, 10) << endl; } void test02() { myPrint myprint; myprint(“hello”); myprint(“hello”); myprint(“hello”); cout << “调用次数:” << myprint.count << endl; } //函数对象可以作为参数传递 void doPrint(myPrint& mp, string test) { mp(test); } void test03() { myPrint myprint; doPrint(myprint, “hello C++”); } int main() { test03(); }