0% found this document useful (0 votes)
28 views

IBM CPP

Virtual base classes allow derived classes that share a common base class to only have one instance of that base class. This is done by declaring the base class as virtual. For example, a class D derived from classes B and C, which both inherit from a common base A, will only have one instance of A instead of two if A is declared as a virtual base class. A derived class can have both virtual and nonvirtual base classes, which will result in some base classes being shared and others being distinct instances.

Uploaded by

Nagaraju Sreeram
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

IBM CPP

Virtual base classes allow derived classes that share a common base class to only have one instance of that base class. This is done by declaring the base class as virtual. For example, a class D derived from classes B and C, which both inherit from a common base A, will only have one instance of A instead of two if A is declared as a virtual base class. A derived class can have both virtual and nonvirtual base classes, which will result in some base classes being shared and others being distinct instances.

Uploaded by

Nagaraju Sreeram
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Virtual base classes (C++ only) http://publib.boulder.ibm.com/infocenter/cellcomp/...

Virtual base classes (C++ only)


Suppose you have two derived classes B and C that have a common base class A, and you also have another class
D that inherits from B and C. You can declare the base class A as virtual to ensure that B and C share the same
subobject of A.

In the following example, an object of class D has two distinct subobjects of class L, one through class B1 and
another through class B2. You can use the keyword virtual in front of the base class specifiers in the base lists
of classes B1 and B2 to indicate that only one subobject of type L, shared by class B1 and class B2, exists.

For example:

class L { /* ... */ }; // indirect base class


class B1 : virtual public L { /* ... */ };
class B2 : virtual public L { /* ... */ };
class D : public B1, public B2 { /* ... */ }; // valid

Using the keyword virtual in this example ensures that an object of class D inherits only one subobject of class
L.

A derived class can have both virtual and nonvirtual base classes. For example:

class V { /* ... */ };
class B1 : virtual public V { /* ... */ };
class B2 : virtual public V { /* ... */ };
class B3 : public V { /* ... */ };
class X : public B1, public B2, public B3 { /* ... */
};

In the above example, class X has two subobjects of class V, one that is shared by classes B1 and B2 and one
through class B3.

Parent topic: Multiple inheritance (C++ only)

Related reference

1 of 2 10/12/10 17:09
Virtual base classes (C++ only) http://publib.boulder.ibm.com/infocenter/cellcomp/...

Derivation (C++ only)

[ Provide feedback ]

2 of 2 10/12/10 17:09

You might also like