这是C/C++中,static变量和自动变量的特性。
// test.cpp
static int a = 1; // 文件作用域的static变量,会在main之前初始化
void test()
{
static int b = 2; // 函数作用域的static变量,在第一次进入test时初始化一次
int c = 3; // 自动变量:每次进入test,都初始化一次
cout << "b = " << b++ << endl;
cout << "c = " << c++ << endl;
}
int main()
{
cout << "main" << endl;
test();
test();
return 0;
}
这是c语言的语法,static修饰的变量只能初始化1次,然后编译器就会忽略掉该定义语句。