C++学习篇:static局部静态对象
- #include <iostream>
- using namespace std;
-
- size_t count_calls()
- {
- static size_t ctr = 0;
- return ++ctr;
- }
-
- int main()
- {
- for (size_t i = 0; i != 10;i++)
- cout << count_calls() << endl;
- return 0;
- }
程序将输出1~10的数字,程序运行时每次static变量都会保存上一次的值。
推荐阅读