//vs2013下测试
#include <iostream>
int g()
{
std::cout << "g()" << std::endl;
return 2;
}
class a{
public:
a(){ std::cout << "a()" << std::endl; }
};
int f()
{
static int i = g();
static a obj;
return 0;
}
int main(char argc, char *argv[])
{
for (int i = 0; i < 3; ++i)
{
std::cout << "for, i = " << i << std::endl;
f();
}
return 0;
}
输出:
for, i = 0
g()
a()
for, i = 1
for, i = 2
结论:
静态局部变量,第一次创建时初始化,之后不再初始化,包括构造函数和调用函数初始化。