English 中文(简体)
为什么破坏数据成员秩序?
原标题:Why does the order of destruction for data member matter?
  • 时间:2011-01-15 02:42:40
  •  标签:
  • c++

我在一书中看到,各年级数据成员的销毁次序应当按其建筑秩序的相反顺序。 这一规则的理由是什么? 欢迎任何实例。

最佳回答

这在很大程度上是为了一致性。 当多件物体按顺序制造时,总是按相反顺序销毁。 例如,用自动变量考虑以下例子:

{ 
    A a;
    B b(a);
}   // b.~B() is called, then
    // a.~A() is called

此处b > 通过保证物体按其施工的相反次序销毁,C++使物体寿命管理更容易 ×。

在某类数据中,当一名数据成员开始成为另一个数据成员时,你可以参考一名数据成员。 确保物体按反向销毁,你的行为与自动变量相同:

struct S
{
    A a;
    B b;

    S() : a(), b(a) { }
};

指出,拥有数据成员相互参照通常不是一个好的想法,但它既可能也偶尔有用。

问题回答

我认为也许你们会误解。 不是成员should 页: 1

在罕见的情况下,了解销毁物品的顺序可能很重要。

不管怎么说,按照销毁物体的命令,你无法做任何事情,但知道它会发生什么。

C++的终身时间尽可能宽松。 虽然数据成员通常很少,但有可能直接依赖对方(例如通过一个点到另一个点)或间接(例如,两者都取决于一个全球性的,如书面输出到梯度),但即使它们没有定点,也只能要求有具体的命令,而且nes令如何比其他命令更适合其他语文工作(例如终身功能)。

当然,根据标准中的“适用”规则,如果汇编者/实施者能够确定用户代码不能遵守重订顺序,那么它就能够照此办理。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签