Play and learn 300 000+ tabs online

Thursday, June 17, 2010

Virtual Base Class How To?

Virtual base class is a base class acts as an indirect base 
for more than one without duplication of its data members.

A single copy of its data members is shared by all the base
classes that use it as a virtual base.

For example:
A
/ \
B C
\ /
D

class A { /* ... */ }; // indirect base class
class B : virtual public A { /* ... */ };
class C : virtual public A { /* ... */ };
class D : public B, public C { /* ... */ }; // valid

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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.