How C++ virtual inheritance is implemented in compilers?

前端 未结 5 558
自闭症患者
自闭症患者 2020-12-24 13:49

How the compilers implement the virtual inheritance?

In the following code:

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

class B : public virtual A {
  publ         


        
5条回答
  •  误落风尘
    2020-12-24 14:32

    It's implementation-dependent. GCC (see this question), for example, will emit two constructors, one with a call to A(1), another one without.

    B1()
    B2() // no A
    

    When B is constructed, the "full" version is called:

    B1():
        A(1)
        B() body
    

    When C is constructed, the base version is called instead:

    C():
        A(3)
        B2()
           B() body
        C() body
    

    In fact, two constructors will be emitted even if there is no virtual inheritance, and they will be identical.

提交回复
热议问题