问题描述
在以下代码中:
class A
{
public:
int x;
A(int x):x(x){}
};
class B: public virtual A
{
public:
B(int x):A(x){}
};
class C: public virtual A
{
public:
C(int x):A(x){}
};
class D: public B, public C
{
public:
D(int x):B(x++), C(x++), A(x++){}
};
两个问题:
- 为什么我需要在D的初始值列表中添加
A(...)
? -
D(int x):B(x ++),C(x ++),A(x ++){}
和x ),C(x ++){}
都给出与cout
- Why do I need to add
A(...)
in D's initializer list? D(int x):B(x++), C(x++), A(x++){}
andD(int x):A(x++), B(x++), C(x++){}
both give the same result withcout<<D(10).x
, why?
推荐答案
由于 A
没有默认构造函数,您需要在中初始化虚拟
A
子对象,
That's because virtual base subobjects must be initialized before all other subobjects. Since A
does not have a default constructor, you need to initialize the virtual A
subobject in D
explicitly and specify which argument you want it to be constructed with.
当 B
如上所述,这是因为虚拟基础子对象首先被初始化。
As explained above, that's because virtual base subobjects are initialized first anyway.
一般来说,类的子对象的初始化顺序决不取决于它们在构造函数初始化列表中出现的顺序。按照C ++ 11标准的第12.6.2 / 10节:
In general, the order of initialization of a class's subobjects never depends on the order in which they appear in your constructor's initialization list. Per paragraph 12.6.2/10 of the C++11 Standard:
- 首先,只有最衍生类别的建构函式(1.8) b $ b它们以基于深度优先的从左到右遍历基本类的有向无环图的顺序出现的顺序,
其中从左到右是出现顺序
— 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.
- 然后,直接基类以声明顺序初始化,因为它们出现在base-specifier-
— 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).
- 非静态数据( 成员按照它们在类定义中声明的顺序被初始化
(同样不考虑 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.
这篇关于C ++虚拟继承初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!