游戏开发论坛

 找回密码
 立即注册
搜索
12
返回列表 发新帖
楼主: wingser

全局对象不会被析构怎么办?

[复制链接]

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2004-12-4 22:06:00 | 显示全部楼层

Re:全局对象不会被析构怎么办?

析构顺序不同...
BTW,楼上真是大手笔啊

28

主题

685

帖子

703

积分

高级会员

Rank: 4

积分
703
发表于 2004-12-4 22:47:00 | 显示全部楼层

与之类似的还有一个,这个好像讨论得蛮热烈的。

362. Order of initialization in instantiation units
Section: 2.1  lex.phases     Status: WP     Submitter: Mark Mitchell     Date: 2 July 2002

[Voted into WP at March 2004 meeting.]

Should this program do what its author obviously expects? As far as I can tell, the standard says that the point of instantiation for Fib<n-1>::Value is the same as the point of instantiation as the enclosing specialization, i.e., Fib<n>::Value. What in the standard actually says that these things get initialized in the right order?

  template<int n>
  struct Fib { static int Value; };

  template <>
  int Fib<0>::Value = 0;

  template <>
  int Fib<1>::Value = 1;

  template<int n>
  int Fib<n>::Value = Fib<n-1>::Value + Fib<n-2>::Value;

  int f ()
  {
    return Fib<40>::Value;
  }

John Spicer: My opinion is that the standard does not specify the behavior of this program. I thought there was a core issue related to this, but I could not find it. The issue that I recall proposed tightening up the static initialization rules to make more cases well defined.

Your comment about point of instantiation is correct, but I don't think that really matters. What matters is the order of execution of the initialization code at execution time. Instantiations don't really live in "translation units" according to the standard. They live in "instantiation units", and the handling of instantiation units in initialization is unspecified (which should probably be another core issue). See 2.1  lex.phases paragraph 8.

Notes from October 2002 meeting:

We discussed this and agreed that we really do mean the the order is unspecified. John Spicer will propose wording on handling of instantiation units in initialization.

Proposed resolution (April 2003):

TC1 contains the following text in 3.6.2  basic.start.init paragraph 1:

Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.
This was revised by issue 270 to read:

Dynamic initialization of an object is either ordered or unordered. Explicit specializations and definitions of class template static data members have ordered initialization. Other class template static data member instances have unordered initialization. Other objects defined in namespace scope have ordered initialization. Objects defined within a single translation unit and with ordered initialization shall be initialized in the order of their definitions in the translation unit. The order of initialization is unspecified for objects with unordered initialization and for objects defined in different translation units.
This addresses this issue but while reviewing this issue some additional changes were suggested for the above wording:

Dynamic initialization of an object is either ordered or unordered. Definitions of explicitly specialized Explicit specializations and definitions of class template static data members have ordered initialization. Other class template static data members (i.e., implicitly or explicitly instantiated specializations) instances have unordered initialization. Other objects defined in namespace scope have ordered initialization. Objects defined within a single translation unit and with ordered initialization shall be initialized in the order of their definitions in the translation unit. The order of initialization is unspecified for objects with unordered initialization and for objects defined in different translation units.

17

主题

454

帖子

470

积分

中级会员

Rank: 3Rank: 3

积分
470
发表于 2004-12-4 23:38:00 | 显示全部楼层

Re:全局对象不会被析构怎么办?

C++不保证 全局对象的初始化顺序.
但是析构的顺序和构造顺序相反.

这点在我做MManager的时候被折腾死了。

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2004-12-4 23:45:00 | 显示全部楼层

Re:全局对象不会被析构怎么办?

thx a lot
it's useful

179

主题

207

帖子

213

积分

中级会员

Rank: 3Rank: 3

积分
213
发表于 2004-12-8 09:12:00 | 显示全部楼层

Re:全局对象不会被析构怎么办?


全局对象会“西狗”的。据我记忆,好像在你的main之前,由编译器作了处理。

179

主题

207

帖子

213

积分

中级会员

Rank: 3Rank: 3

积分
213
发表于 2004-12-8 09:25:00 | 显示全部楼层

Re:全局对象不会被析构怎么办?

我不知道怎样传图片,截了图。

#include<iostream>
using namespace std;
class a
{
public:
a()
{
cout<<"cing\n"<<endl;             //!1
}
~a()
{
cout<<"ding\n"<<endl;
}
};

a a;

int main()                   //!2
{
}



环境:vs2k+3

debug it,首先运行 //!1 ,然后运行//!2, 就是这样子。

在c++builder 5,6下、vs2k,vs2k+2,记忆中也如此。

作程序,凡事debug,要单步的那种。

89

主题

822

帖子

847

积分

高级会员

Rank: 4

积分
847
发表于 2004-12-8 10:43:00 | 显示全部楼层

Re:全局对象不会被析构怎么办?

肯定会析构的,只是说你没看到输出而已,没看到输出并不表示没调用析构函数,可能是由于别的原因

2

主题

33

帖子

33

积分

注册会员

Rank: 2

积分
33
QQ
发表于 2004-12-11 18:19:00 | 显示全部楼层

Re:全局对象不会被析构怎么办?

会析构的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-23 16:33

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表