天天看點

全局對象的構造與析構

#include<iostream>
using namespace std;
class xint
{
private:
	int x;
public:
	xint(int dt){
		x=dt;
		cout<<x<<"constructor"<<endl;
	}
	~xint(){
		cout<<x<<"destructor"<<endl;
	}
};
xint a(1);
xint b(2);
int f()
{
	xint f(7);
	cout<<"f"<<endl;
}
xint c(3);
int main()
{
	xint d(5);
	cout<<"main"<<endl;
	f();
}
xint e(6);
           
全局對象的構造與析構

根據運算結果可知,先按全局對象的定義順序執行其構造函數,然後按函數的調用順序執行局部對象的構造函數和函數體。析構函數按構造函數的逆序執行。

繼續閱讀