C++ primer(第五版) 练习 6.7
题目:
编写一个函数,当它第一次被调用时返回0,以后每次调用返回值加1。
以下是我无脑的答案:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int test()
{
static int jubujingtaibianliang = 0;
return jubujingtaibianliang++;
}
int main()
{
cout << test() << endl;
cout << test() << endl;
cout << test() << endl;
cout << test() << endl;
cout << test() << endl;
return 0;
}
执行结果: