您的位置:首页 > 其它

VC6.0上编译的程序使用不调用全局对象的析构函数

2011-02-20 15:10 453 查看
编译环境如下

编译器:VC 6.0

操作系统:Window 7

测试代码一:

#include <iostream>
#include <string.h>

using namespace std;

class CDemo
{
public:
CDemo(const char* str);
~CDemo();
private:
char name[20];
};

CDemo::CDemo(const char* str)
{
strncpy(name, str, 19);
name[19] = '/0';
cout << "Constructor called for " << name << endl;
}

CDemo::~CDemo()
{
cout << "Destructor called for " << name << endl;
}

CDemo c_global("global object");
static CDemo c_static("static object");

void main(void)
{
CDemo c_local("local object");
}


代码一执行结果:



将代码一中的"using namespace std;"去掉,并把“#include <iostream>”改成"#include <iostream.h>"后的代码二如下:

#include <iostream.h>
#include <string.h>

class CDemo
{
public:
CDemo(const char* str);
~CDemo();
private:
char name[20];
};

CDemo::CDemo(const char* str)
{
strncpy(name, str, 19);
name[19] = '/0';
cout << "Constructor called for " << name << endl;
}

CDemo::~CDemo()
{
cout << "Destructor called for " << name << endl;
}

CDemo c_global("global object");
static CDemo c_static("static object");

void main(void)
{
CDemo c_local("local object");
}


代码二的执行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐