English 中文(简体)
基础级初始器和成员变量初始器的顺序是否重要?
原标题:Does the order of base-class initializers and member variable initializers matter?

初级建筑商的命令是否重要?

因此,我要说:

class MyClass : BaseClass
{
      int a, b, c;

   public:
      MyClass(int);
}

<>strong>e.g:

MyClass::MyClass(int forBase) :
  a(7),
  b(14),
  c(28),
  BaseClass(forBase) { }

<>strong>e.g:

MyClass::MyClass(int forBase) :
  BaseClass(forBase),
  a(7),
  b(14),
  c(28) { }

例1是否与例2不同?

最佳回答

例1是否与例2不同?

<>No。 初始批准令由标准决定,而不是由你写初审员的顺序决定:

[C++11: 12.6.2/10]: In a non-delegating constructor, initialization proceeds in the following order:

  • First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
  • Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
  • Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
  • Finally, the compound-statement of the constructor body is executed.

事实上,如果你在任何其他命令中写到这些命令,则取决于另一个命令,。 请您:

struct T {
   std::vector<int> v;
   int w;

   T(int w) : w(w), v(0, w) {}
};

int main() {
   T t(3);
}

// g++ 4.1.2:
// t.cpp: In constructor  T::T(int) :
// Line 3: warning:  T::w  will be initialized after
// Line 2: warning:    __gnu_debug_def::vector<int, std::allocator<int> > T::v 
// Line 5: warning:   when initialized here
问题回答

该命令并不涉及汇编者(初始化令总是以基级为主,总是按衍生顺序为基级,而按申报顺序为成员),但对于读者来说是有意义的: 如果你给初始执行者的命令与执行命令不一致,那是令人困惑的。 虽然在大多数情况下,它只是 t事,但在某些情况下,你可以产生微妙的 b,例如。

struct Derived: Base
{
  int member;
  Derived();
}

Derived::Derived():
  member(3),
  Base(member) // This is executed *before* member is initialized!
{
}

如果在正确的顺序中给出初始者的话,这种 b子就会更加明显:

Derived::Derived():
  Base(member), // Now we see immediately that member is uninitialized
  member(3),
{
}

这的确使你在建筑工程初始化清单中列出了初步设计者。 成员按申报的顺序入选,成员先入选。

However, listing initializers in a different order that that can bite you if a subobject s initial value depends on the values of other subobjects.

class A
{
  int y, x;
  A(int x_value): x(x_value), y(x) {}
};

自X之前开始实行,它具有垃圾价值,最初的制片人名单的顺序只是暗中。 因此,这理应受到汇编者的警告。





相关问题
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?

热门标签