C++中的static变量调用析构函数问题
#include <iostream.h>
class Football{
public:
Football(){cout<<"a footbal is created !"<<endl;}
~Football(){cout<<"a football is destroyed !"<<endl;}
};
void func(int i)
{
static Football f1;
Football f2;
cout<<"i="<<i<<endl;
}
int main()
{
func(10);
func(20);
return 0;
}
====运行结果=
//fun(10)
a footbal is created ! //f1(静态)调用构造函数
a footbal is created ! //f2调用构造函数
i=10 //i=10
a football is destroyed ! //f2调用析构函数
//fun(20)
a footbal is created ! //f2调用构造函数
i=20
a football is destroyed ! // f2调用析构函数
a football is destroyed ! //f1(静态)调用析构函数
小结:c++里的static变量构造一次,在最后析构一次。