天天看点

探讨全局变量的析构顺序

                                                                        朱金灿

前言:最近回答了网友一个问题,当然我不是C++高手,我仅是提出我的猜想和大家交流。

  经典的C++的教科书都这样写着:全局对象在调用 main之前初始化, 在退出main之后析构。但是大家请看下面这样一个程序:

#include <cstdlib>

#include <iostream>

#include <conio.h>

class gb

{

public:

gb::gb()

{

std::cout<<"start"<<std::endl;

};

gb::~gb()

{

std::cout<<"end"<<std::endl;

getch();

};

};

gb a;

int main()

{

std::cout<<"This is in main"<<std::endl;

return 0;

}

    大家认为输出是什么呢?大家可能会说:这不是很简单吗?

Start

This is in main

End

     实际上事情并没有这么简单。在dev-c++-4.9.9.2的工程上,输出结果是顺理成章的:

Start

This is in main

End

在VC6.0上建一个控制台工程,把代码拷贝进去,发现运行结果却是:

Start

This is in main

    当时很感疑惑,难道gb类对象a没有执行析构函数?思考了一会,决定试验一下。把代码稍为修改一下,把cout全部换为C语言的printf()函数,如下:

#include <cstdlib>

#include <iostream>

#include <conio.h>

#include <stdio.h>

class gb

{

public:

gb::gb()

{

//std::cout<<"start"<<std::endl;

printf("start/n");

};

gb::~gb()

{

// std::cout<<"end"<<std::endl;

printf("end/n");

getch();

};

};

gb a;

int main()

{

std::cout<<"This is in main"<<std::endl;

return 0;

}

运行结果是:

Start

This is in main

End

这说明析构函数是有执行的。下面提出我的一个猜想:cout作为一个iostream类的对象,在退出main函数后比gb类对象a先执行析构函数,故无法输出End。值得注意这种全局变量的析构顺序是和编译器相关的,在VC上是cout ——〉gb a,而在dev-c++-4.9.9.2是gb a——〉cout。实际上C++标准只规定了同一个cpp文件里的全局变量的构造和析构顺序,