Virtual inheritance

Virtual inheritance

: "For inheritance of virtual functions, see virtual function."

In the C++ programming language, virtual inheritance is a kind of inheritance that solves some of the problems caused by multiple inheritance (particularly the "diamond problem") by clarifying ambiguity over which ancestor class members to use. It is used when inheritance is representing restrictions of a set rather than composition of parts. A multiply-inherited base class is denoted as virtual with the virtual keyword.

The problem

Consider the following class hierarchy.class Animal { public: virtual void eat();};

class Mammal : public Animal { public: virtual Color getHairColor();};

class WingedAnimal : public Animal { public: virtual void flap();};

// A bat is a winged mammalclass Bat : public Mammal, public WingedAnimal {};

Bat bat;But how does bat eat()? As declared above, a call to bat.eat() is ambiguous. One would have to call either bat.WingedAnimal::Animal::eat() or bat.Mammal::Animal::eat().

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

Class representation

Before going further it is helpful to consider how classes are represented in C++. In particular, inheritance is simply a matter of putting parent and child class one after the other in memory. Thus Bat is really (Animal,Mammal,Animal,WingedAnimal,Bat) which makes Animal duplicated, causing the ambiguity.

olution

We can redeclare our classes as follows:// Two classes virtually inheriting Animal:class Mammal : public virtual Animal { public: virtual Color getHairColor();};

class WingedAnimal : public virtual Animal { public: virtual void flap();};

// A bat is still a winged mammalclass Bat : public Mammal, public WingedAnimal {};Now the Animal portion of Bat::WingedAnimal is the "same" Animal as the one used by Bat::Mammal, which is to say that a Bat has only one Animal in its representation and so a call to Bat::eat() is unambiguous.

This is implemented by providing Mammal and WingedAnimal with a vtable pointer since, e.g., the memory offset between the beginning of a Mammal and of its Animal part is unknown until runtime. Thus Bat becomes (vtable*,Mammal,vtable*,WingedAnimal,Bat,Animal). Two vtable pointers per object, so the object size increased by two pointers, but now there is only one Animal and no ambiguity. There are two vtables pointers: one per inheritance hierarchy that virtually inherits Animal: One for Mammal and one for WingedAnimal. All objects of type Bat will have the same vtable *'s, but each Bat object will contain its own unique Animal object. If another class inherits Mammal, such as Squirrel, then the vtable* in the Mammal object in a Squirrel will be different from the vtable* in the Mammal object in a Bat, although they can still be essentially the same in the special case that the squirrel part of the object has the same size as the Bat part, because then the distance from the Mammal to the Animal part is the same. The vtables are not really the same, but all essential information in them (the distance) is.

ee also

*Object association


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Virtual method table — A virtual method table, virtual function table, dispatch table, or vtable, is a mechanism used in programming language to support dynamic dispatch (or run time method binding).Suppose a program contains several classes in an inheritance hierarchy …   Wikipedia

  • Virtual function — In object oriented programming, a virtual function or virtual method is one whose behavior is defined within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object… …   Wikipedia

  • Inheritance (object-oriented programming) — In object oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by …   Wikipedia

  • Inheritance (computer science) — In object oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The inheritance concept was invented in 1967 for Simula. [ [http://heim.ifi.uio.no/… …   Wikipedia

  • Virtual tax — is a proposed tax on internet gamers for items bought or traded solely within the virtual world (Internet game worlds). [ [http://money.howstuffworks.com/virtual tax.htm Does the IRS really want your World of Warcraft gold?] , by Julia Layton,… …   Wikipedia

  • Multiple inheritance — is a feature of some object oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass. Languages that support multiple inheritance include: C++, Common Lisp (via CLOS), EuLisp (via… …   Wikipedia

  • Composition over inheritance — in object oriented programming is a technique by which classes may achieve polymorphic behavior and code reuse by containing other classes which implement the desired functionality instead of through inheritance.[1] This technique is also… …   Wikipedia

  • C++ — The C++ Programming Language, written by its architect, is the seminal book on the language. Paradigm(s) Multi paradigm:[1] procedural …   Wikipedia

  • Comparison of Java and C++ — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Diamond problem — A diamond class inheritance diagram. In object oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”