Virtual Inheritance - The Problem

The Problem

Consider the following class hierarchy.

class Animal { public: virtual void eat; }; class Mammal : public Animal { public: virtual void breathe; }; class WingedAnimal : public Animal { public: virtual void flap; }; // A bat is a winged mammal class Bat : public Mammal, public WingedAnimal { }; Bat bat;

As declared above, a call to bat.eat is ambiguous because there are two Animal (indirect) base classes in Bat, so any Bat object has two different Animal base class subobjects. So an attempt to directly bind a reference to the Animal subobject of a Bat object would fail, since the binding is inherently ambiguous:

Bat b; Animal &a = b; // error: which Animal subobject should a Bat cast into, // a Mammal::Animal or a WingedAnimal::Animal?

To disambiguate, one would have to explicitly convert bat to either base class subobject:

Bat b; Animal &mammal = static_cast (b); Animal &winged = static_cast (b);

In order to call eat, the same disambiguation is needed: static_cast(bat).eat or static_cast(bat).eat.

In this case, the double inheritance of Animal is probably unwanted, as we want to model that the relation (Bat is an Animal) exists only once; that a Bat is a Mammal and is a WingedAnimal does not imply that it is an Animal twice: an Animal base class corresponds to a contract that Bat implements (the "is a" relationship above really means "implements the requirements of"), and a Bat only implements the Animal contract once. The real world meaning of "is a only once" is that Bat should have only one way of implementing eat, not two different ways, depending on whether the Mammal view of the Bat is eating, or the WingedAnimal view of the Bat. (In the first code example we see that eat is not overridden in either Mammal or WingedAnimal, so the two Animal subobjects will actually behave the same, but this is just a degenerate case, and that does not make a difference from the C++ point of view.)

This situation is sometimes referred to as diamond inheritance (see Diamond problem) because the inheritance diagram is in the shape of a diamond. Virtual inheritance can help to solve this problem.

Read more about this topic:  Virtual Inheritance

Famous quotes containing the word problem:

    A serious problem in America is the gap between academe and the mass media, which is our culture. Professors of humanities, with all their leftist fantasies, have little direct knowledge of American life and no impact whatever on public policy.
    Camille Paglia (b. 1947)

    We have heard all of our lives how, after the Civil War was over, the South went back to straighten itself out and make a living again. It was for many years a voiceless part of the government. The balance of power moved away from it—to the north and the east. The problems of the north and the east became the big problem of the country and nobody paid much attention to the economic unbalance the South had left as its only choice.
    Lyndon Baines Johnson (1908–1973)