With multiple inheritance (MI), you may have a class hierarchy with more than one path between a base class and a derived class. For example:
class basic_ios { ... };
class basic_istream: public basic_ios { ... };
class basic_ostream: public basic_ios { ... };
class basic_iostream: public basic_istream, public basic_ostream
{ ... };
To restrict inclusion of members of class basic_istream twice into class basic_iostream, use virtual inheritance. Declare the above classes as:
class basic_ios { ... };
class basic_istream: virtual public basic_ios { ... };
class basic_ostream: virtual public basic_ios { ... };
class basic_iostream: public basic_istream, public basic_ostream
{ ... };
But try to avoid virtual inheritance because:
- objects created using virtual inheritance are generally larger.
- Access to data members in virtual base classes is also slower than those in non-virtual base classes.
Cheers