/*
测试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下对象的析构顺序与创建顺序是相反的.
其它编译环境未能测试, 有兴趣者可自行测试.
*/