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

    技术2026-09-11  18

    编译环境如下

    编译器: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"); }

    代码二的执行结果:

     

     

    最新回复(0)