其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
测试C++对象析构顺序是否与构造顺序相关的代码

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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




相关文章

相关软件