Multiple (diamond) inheritance compiles without “virtual”, but doesn't with

前端 未结 2 1334
北海茫月
北海茫月 2020-12-06 17:04

Given the following code (without virtual inheritance) :

class A
{
public:
    virtual void f() = 0;
};

class B : public A
{
 public:
    virtual void f()          


        
相关标签:
2条回答
  • 2020-12-06 17:43

    Your first scenario hierarchy corresponds to:

        F()   F()
         A     A
         |     |
     F() B     C F()
          \   /
            D 
    

    Where D is not abstract, because there are two A subobjects in an object of type D: One that is made concrete by B through the lattice of B, and another that is made concrete through the lattice of C.

    Unless you try to invoke the function F() on object of D there will not be any ambiguity.

    Your second scenario hierarchy corresponds to:

           F()  
            A
          /   \
     F() B     C F()
          \   /
            D  
    

    In this scenario, the object D has a single Base class A sub object, and it must override and provide implementation of the pure virtual function in that subobject.


    Herb Sutter's articles in Guru Of The Week(GOTW) are a nice read for Multiple Inheritance:

    1. Multiple Inheritance Part I
    2. Multiple Inheritance Part II
    3. Multiple Inheritance Part III
    0 讨论(0)
  • 2020-12-06 17:53

    With the virtual inheritance a D object has a single base-class A sub-object. This single sub-object can’t have two different implementations of a virtual function. In contrast, without virtual inheritance a D object has two distinct base-class A sub-objects, each with its own implementation of the function (which is OK until you try to call it on a D object, at which point you need to indicate which one you want).

    Cheers & hth.

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