C++ virtual inheritance initializer list

前端 未结 2 1064
清歌不尽
清歌不尽 2021-01-05 03:50

in the following code:

class A
{
public:
    int x;
    A(int x):x(x){}
};

class B: public virtual A
{
public:
    B(int x):A(x){}
};
class C: public virtua         


        
相关标签:
2条回答
  • 2021-01-05 04:14

    Why do I need to add A(...) in D's initializer list?

    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.

    When the constructors of the B and C base subobjects are executed, they won't have a base A subobject to initialize (that has been done before already). Therefore, the arguments they pass to A's constructor are irrelevant.

    D(int x):B(x++), C(x++), A(x++){} and D(int x):A(x++), B(x++), C(x++){} both give the same result with cout<<D(10).x, why?

    As explained above, that's because virtual base subobjects are initialized first anyway.

    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:

    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.

    0 讨论(0)
  • 2021-01-05 04:28

    Virtual base classes are only initialised by the most-derived class. That is, if create an instance of D in your example, A will only be initialised by its occurence in the mem-initialiser list of D. Its occurence in the mem-initialiser lists of B and C is simply ignored.

    That is also why you have to initialise A in D: A doesn't have a default ctor, so D must know how to initialise it.

    0 讨论(0)
提交回复
热议问题