count累加器,看看加法仿函数一共调用了多少次。
//请问为什么count的输出是0
#include<iostream>
using namespace std;
class MyAdd
{
public:
int operator()(int a, int b)
{
return a + b;
count++;
}
private:
static int count;
friend void showCount();
};
int MyAdd::count = 0;
void showCount()
{
cout << MyAdd::count << endl;
}
class MyPrint
{
public:
void operator()(string text)const
{
cout << text << endl;
}
};
void doPrint(const MyPrint& m, string text)
{
m(text);
}
int main()
{
MyAdd m;
cout << m(30, m(10, 20)) << endl;
showCount();
MyPrint m2;
doPrint(m2,"Hello World");
}