#代码如下:
#include <iostream>
using namespace std;
void testStatic() {
static int count = 0; // static 变量只初始化一次,且值会保留到下一次调用
count++;
cout << "调用次数: " << count << endl;
}
int main() {
testStatic(); // 第一次调用
testStatic(); // 第二次调用
testStatic(); // 第三次调用
return 0;
}
运行结果如下: