Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Given the following code (without virtual inheritance) :

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

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

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

class D : public B, public C
{

/* some code */
};


int main()
{
    D d;
    return 0;
}

the code compile.

On the other hand , here :

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

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

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

class D : public B, public C
{
    /* some code */
};


int main()
{
    D d;
    return 0;
}

The compiler presents a compilation error:

no unique final overrider for 'virtual void A::f()' in 'D' . 

Why is it different in the second code ?

like image 723
Ron_s Avatar asked Aug 28 '11 08:08

Ron_s


People also ask

What is Diamond problem in multiple inheritance?

The "diamond problem" (sometimes referred to as the "Deadly Diamond of Death") is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C.

How does virtual inheritance solve the diamond problem?

Virtual inheritance solves the classic “Diamond Problem”. It ensures that the child class gets only a single instance of the common base class. In other words, the Snake class will have only one instance of the LivingThing class. The Animal and Reptile classes share this instance.

What is Diamond problem in inheritance in C++?

The diamond problem The diamond problem occurs when two superclasses of a class have a common base class. For example, in the following diagram, the TA class gets two copies of all attributes of Person class, this causes ambiguities. For example, consider the following program. CPP.

Does C++ allow multiple inheritance?

C++ allows a special kind of inheritance known as multiple inheritance. While most object oriented languages support inheritance, not all of them support multiple inheritance. (Java is one such example). Multiple Inheritance simply means that a class can inherit properties from more than one base class.


2 Answers

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
like image 175
Alok Save Avatar answered Oct 23 '22 21:10

Alok Save


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.

like image 26
Cheers and hth. - Alf Avatar answered Oct 23 '22 22:10

Cheers and hth. - Alf