例如,存在一个类的全局对象,或者存在一个普通全局变量,但其使用了一个函数来为其赋值,这种情况下就会先执行类对象的构造函数和全局变量的赋值函数
请看以下代码:
#include <iostream>
using namespace std;
int fun(int n)
{
cout<<n*n<<endl;
return n*n;
}
int m = fun(10);
class MyTest
{
public:
MyTest()
{
cout<<"creat MyTest Object"<<endl;
}
~MyTest()
{
cout<<"release myTest Object"<<endl;
}
};
MyTest mytest;
int main()
{
cout<<"hello world!"<<endl;
return 0;
}
执行结果是: