Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, what is a virtual base class?

I want to know what a "virtual base class" is and what it means.

Let me show an example:

class Foo { public:     void DoSomething() { /* ... */ } };  class Bar : public virtual Foo { public:     void DoSpecific() { /* ... */ } }; 
like image 427
popopome Avatar asked Aug 22 '08 01:08

popopome


People also ask

What is meant by virtual base class?

Virtual base class in C++ Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class appearing in an inheritance hierarchy when using multiple inheritances. Need for Virtual Base Classes: Consider the situation where we have one class A .

What is virtual base class and virtual function?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

What is virtual base function?

A virtual function is a member function in the base class that we expect to redefine in derived classes. Basically, a virtual function is used in the base class in order to ensure that the function is overridden. This especially applies to cases where a pointer of base class points to an object of a derived class.

What is the difference between a virtual base class and a normal base class?

Base class having virtual function can be instantiated i.e. its object can be made. Base class having pure virtual function becomes abstract i.e. it cannot be instantiated.


1 Answers

Virtual base classes, used in virtual inheritance, is a way of preventing multiple "instances" of a given class appearing in an inheritance hierarchy when using multiple inheritance.

Consider the following scenario:

class A { public: void Foo() {} }; class B : public A {}; class C : public A {}; class D : public B, public C {}; 

The above class hierarchy results in the "dreaded diamond" which looks like this:

  A  / \ B   C  \ /   D 

An instance of D will be made up of B, which includes A, and C which also includes A. So you have two "instances" (for want of a better expression) of A.

When you have this scenario, you have the possibility of ambiguity. What happens when you do this:

D d; d.Foo(); // is this B's Foo() or C's Foo() ?? 

Virtual inheritance is there to solve this problem. When you specify virtual when inheriting your classes, you're telling the compiler that you only want a single instance.

class A { public: void Foo() {} }; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C {}; 

This means that there is only one "instance" of A included in the hierarchy. Hence

D d; d.Foo(); // no longer ambiguous 

This is a mini summary. For more information, have a read of this and this. A good example is also available here.

like image 136
OJ. Avatar answered Sep 29 '22 16:09

OJ.