测试C++对象析构顺序是否与构造顺序相关的代码

王朝c/c++·作者佚名  2006-01-09
宽屏版  字体: |||超大  

/*

测试C++对象析构顺序是否与构造顺序相关

张晓辉 2004-12-12

*/

#include <iostream>

using namespace std;

class A{

public:

A(){

order=++count;

cout<<"constructing the "<<order<<"th object."<<endl;

}

~A(){

cout<<"destroying the "<<order<<"th object of "<<count<<" objects."<<endl;

}

private:

static int count;

int order;

};

int A::count=0;

int main(){

A a[10];

return 0;

}

/* VC++ 6.0下本程序的输入出下:

constructing the 1th object.

constructing the 2th object.

constructing the 3th object.

constructing the 4th object.

constructing the 5th object.

constructing the 6th object.

constructing the 7th object.

constructing the 8th object.

constructing the 9th object.

constructing the 10th object.

destroying the 10th object of 10 objects.

destroying the 9th object of 10 objects.

destroying the 8th object of 10 objects.

destroying the 7th object of 10 objects.

destroying the 6th object of 10 objects.

destroying the 5th object of 10 objects.

destroying the 4th object of 10 objects.

destroying the 3th object of 10 objects.

destroying the 2th object of 10 objects.

destroying the 1th object of 10 objects.

*/

/*

由此可见, 在VC++6.0下对象的析构顺序与创建顺序是相反的.

其它编译环境未能测试, 有兴趣者可自行测试.

*/

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有