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

前端 未结 2 1454
北海茫月
北海茫月 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: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.

提交回复
热议问题