SlideShare a Scribd company logo
1 of 5
Download to read offline
Wade	Not	in	Unknown	Waters.	Part	Four.
Author: Andrey Karpov
Date: 15.07.2013
This time we will discuss virtual inheritance in C++ and find out why one should be very careful using it.
See other articles of this series: N1, N2, N3.
Initialization of Virtual Base Classes
At first let's find out how classes are allocated in memory without virtual inheritance. Have a look at this
code fragment:
class Base { ... };
class X : public Base { ... };
class Y : public Base { ... };
class XY : public X, public Y { ... };
It's pretty clear: members of the non-virtual base class 'Base' are allocated as common data members of
a derived class. It results in the 'XY' object containing two independent 'Base' subobjects. Here is a
scheme to illustrate that:
Figure 1. Multiple non-virtual inheritance.
When we deal with virtual inheritance, an object of a virtual base class is included into the object of a
derived class only once. Figure 2 shows the structure of the 'XY' object in the code fragment below.
class Base { ... };
class X : public virtual Base { ... };
class Y : public virtual Base { ... };
class XY : public X, public Y { ... };
Figure 2. Multiple virtual inheritance.
It is at the end of the 'XY' object that memory for the shared subobject 'Base' is most probable to be
allocated. The exact implementation of the class depends on the compiler. For example, the classes 'X'
and 'Y' may store pointers to the shared object 'Base'. But as far as I understand, this practice is out of
use nowadays. A reference to a shared subobject is rather implemented through offset or as
information stored in the virtual function table.
The "most derived" class 'XY' alone knows where exactly a subobject of the virtual base class 'Base' is to
be allocated. That's why it is the most derived class which is responsible for initializing all the subobjects
of virtual base classes.
'XY' constructors initialize the 'Base' subobject and pointers to it in 'X' and 'Y'. After that, all the rest
members of the classes 'X', 'Y' and 'XY' are initialized.
Once the 'XY' constructor has initialized the 'Base' subobject, the 'X' and 'Y' constructors are not allowed
to re-initialize it. The particular way it will be done depends on the compiler. For example, it can pass a
special additional argument into the 'X' and 'Y' constructors to tell them not to initialize the 'Base' class.
Now the most interesting thing which causes much confusion and a lot of mistakes. Have a look at the
following constructors:
X::X(int A) : Base(A) {}
Y::Y(int A) : Base(A) {}
XY::XY() : X(3), Y(6) {}
What number will the base class's constructor take as an argument - 3 or 6? None!
The constructor 'XY' initializes the virtual subobject 'Base' yet does that implicitly. It is the 'Base'
constructor which is called by default.
As the 'XY' constructor calls the 'X' or 'Y' constructor, it doesn't re-initialize 'Base'. That's why 'Base' is
not being called with an argument passed into it.
Troubles with virtual base classes don't end here. Besides constructors, there are also assignment
operators. If I'm not mistaken, the standard tells us that an assignment operator generated by the
compiler may assign values to a subobject of a virtual base class multiple times or once. So, you just
don't know how many times the 'Base' object will be copied.
If you implement your own assignment operator, make sure you have prevented multiple copying of the
'Base' object. The following code fragment is incorrect:
XY &XY::operator =(const XY &src)
{
if (this != &src)
{
X::operator =(*this);
Y::operator =(*this);
....
}
return *this;
}
This code leads to double copying of the 'Base' object. To avoid this, we should add special functions
into the 'X' and 'Y' classes to prevent copying of the 'Base' class's members. The contents of the 'Base'
class are copied just once, in the same code fragment. This is the fixed code:
XY &XY::operator =(const XY &src)
{
if (this != &src)
{
Base::operator =(*this);
X::PartialAssign(*this);
Y::PartialAssign(*this);
....
}
return *this;
}
This code will work well, but it still doesn't look nice and clear. That's the reason why programmers are
recommended to avoid multiple virtual inheritance.
Virtual Base Classes and Type Conversion
Because of the specifics of how virtual base classes are allocated in memory, you can't perform type
conversions like this one:
Base *b = Get();
XY *q = static_cast<XY *>(b); // Compilation error
XY *w = (XY *)(b); // Compilation error
A persistent programmer, though, will achieve that by employing the operator 'reinterpret_cast':
XY *e = reinterpret_cast<XY *>(b);
However, the result will hardly be of any use. The address of the beginning of the 'Base' object will be
interpreted as a beginning of the 'XY' object, which is quite a different thing. See Figure 3 for details.
The only way to perform a type conversion is to use the operator dynamic_cast. But using dynamic_cast
too often makes the code smell.
Figure 3. Type conversion.
Should We Abandon Virtual Inheritance?
I agree with many authors that one should avoid virtual inheritance by all means, as well as common
multiple inheritance.
Virtual inheritance causes troubles with object initialization and copying. Since it is the "most derived"
class which is responsible for these operations, it has to be familiar with all the intimate details of the
structure of base classes. Due to this, a more complex dependency appears between the classes, which
complicates the project structure and forces you to make some additional revisions in all those classes
during refactoring. All this leads to new bugs and makes the code less readable.
Troubles with type conversions may also be a source of bugs. You can partly solve the issues by using
the dynamic_cast operator. But it is too slow, and if you have to use it too often in your code, it means
that your project's architecture is probably very poor. Project structure can be almost always
implemented without multiple inheritance. After all, there are no such exotica in many other languages,
and it doesn't prevent programmers writing code in these languages from developing large and complex
projects.
We cannot insist on total refusal of virtual inheritance: it may be useful and convenient at times. But
always think twice before making a heap of complex classes. Growing a forest of small classes with
shallow hierarchy is better than handling a few huge trees. For example, multiple inheritance can be in
most cases replaced by object composition.
Good Sides of Multiple Inheritance
OK, we now understand and agree with the criticism of multiple virtual inheritance and multiple
inheritance as such. But are there cases when it can be safe and convenient to use?
Yes, I can name at least one: Mix-ins. If you don't know what it is, see the book "Enough Rope to Shoot
Yourself in the Foot" [3]
A mix-in class doesn't contain any data. All its functions are usually pure virtual. It has no constructor,
and even when it has, it doesn't do anything. It means that no troubles will occur when creating or
copying these classes.
If a base class is a mix-in class, assignment is harmless. Even if an object is copied many times, it doesn't
matter: the program will be free of it after compilation.
References
1. Stephen C. Dewhurst. "C++ Gotchas: Avoiding Common Problems in Coding and Design". -
Addison-Wesley Professional. - 352 pages; illustrations. ISBN-13: 978-0321125187. (See gotchas
45 and 53).
2. Wikipedia. Object composition.
3. Allen I. Holub. "Enough Rope to Shoot Yourself in the Foot". (You can easily find it on the
Internet. Start reading at section 101 and further on).

More Related Content

Viewers also liked

Viewers also liked (16)

What comments hide
What comments hideWhat comments hide
What comments hide
 
How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.
 
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-StudioComparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
 
Cppcheck
CppcheckCppcheck
Cppcheck
 
Static analysis should be used regularly
Static analysis should be used regularlyStatic analysis should be used regularly
Static analysis should be used regularly
 
What do static analysis and search engines have in common? A good "top"!
What do static analysis and search engines have in common? A good "top"!What do static analysis and search engines have in common? A good "top"!
What do static analysis and search engines have in common? A good "top"!
 
Creating, debugging and deploying extension packages for Microsoft Visual Stu...
Creating, debugging and deploying extension packages for Microsoft Visual Stu...Creating, debugging and deploying extension packages for Microsoft Visual Stu...
Creating, debugging and deploying extension packages for Microsoft Visual Stu...
 
Errors detected in C++Builder
Errors detected in C++BuilderErrors detected in C++Builder
Errors detected in C++Builder
 
Visual C++ project model
Visual C++ project modelVisual C++ project model
Visual C++ project model
 
PVS-Studio advertisement - static analysis of C/C++ code
PVS-Studio advertisement - static analysis of C/C++ codePVS-Studio advertisement - static analysis of C/C++ code
PVS-Studio advertisement - static analysis of C/C++ code
 
The D language comes to help
The D language comes to helpThe D language comes to help
The D language comes to help
 
100% code coverage by static analysis - is it that good?
100% code coverage by static analysis - is it that good?100% code coverage by static analysis - is it that good?
100% code coverage by static analysis - is it that good?
 
Difficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usabilityDifficulties of comparing code analyzers, or don't forget about usability
Difficulties of comparing code analyzers, or don't forget about usability
 
Farewell to #define private public
Farewell to #define private publicFarewell to #define private public
Farewell to #define private public
 
Wade not in unknown waters. Part one.
Wade not in unknown waters. Part one.Wade not in unknown waters. Part one.
Wade not in unknown waters. Part one.
 
Lesson 7. The issues of detecting 64-bit errors
Lesson 7. The issues of detecting 64-bit errorsLesson 7. The issues of detecting 64-bit errors
Lesson 7. The issues of detecting 64-bit errors
 

Similar to Wade Not in Unknown Waters. Part Four.

How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
Tyagi2636
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Ankur Dongre
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Ankur Dongre
 
Read carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdfRead carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdf
rupeshmehta151
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
Katy Allen
 

Similar to Wade Not in Unknown Waters. Part Four. (20)

How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
 
Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?
 
LEARN C#
LEARN C#LEARN C#
LEARN C#
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
C# classes
C#   classesC#   classes
C# classes
 
Read carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdfRead carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdf
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Virtual Method Table and accident prevention
Virtual Method Table and accident preventionVirtual Method Table and accident prevention
Virtual Method Table and accident prevention
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
Highly Strung
Highly StrungHighly Strung
Highly Strung
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
 
Ad507
Ad507Ad507
Ad507
 
Introduction to es6
Introduction to es6Introduction to es6
Introduction to es6
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
 
Latest C++ Interview Questions and Answers
Latest C++ Interview Questions and AnswersLatest C++ Interview Questions and Answers
Latest C++ Interview Questions and Answers
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

Wade Not in Unknown Waters. Part Four.

  • 1. Wade Not in Unknown Waters. Part Four. Author: Andrey Karpov Date: 15.07.2013 This time we will discuss virtual inheritance in C++ and find out why one should be very careful using it. See other articles of this series: N1, N2, N3. Initialization of Virtual Base Classes At first let's find out how classes are allocated in memory without virtual inheritance. Have a look at this code fragment: class Base { ... }; class X : public Base { ... }; class Y : public Base { ... }; class XY : public X, public Y { ... }; It's pretty clear: members of the non-virtual base class 'Base' are allocated as common data members of a derived class. It results in the 'XY' object containing two independent 'Base' subobjects. Here is a scheme to illustrate that: Figure 1. Multiple non-virtual inheritance. When we deal with virtual inheritance, an object of a virtual base class is included into the object of a derived class only once. Figure 2 shows the structure of the 'XY' object in the code fragment below. class Base { ... }; class X : public virtual Base { ... }; class Y : public virtual Base { ... }; class XY : public X, public Y { ... };
  • 2. Figure 2. Multiple virtual inheritance. It is at the end of the 'XY' object that memory for the shared subobject 'Base' is most probable to be allocated. The exact implementation of the class depends on the compiler. For example, the classes 'X' and 'Y' may store pointers to the shared object 'Base'. But as far as I understand, this practice is out of use nowadays. A reference to a shared subobject is rather implemented through offset or as information stored in the virtual function table. The "most derived" class 'XY' alone knows where exactly a subobject of the virtual base class 'Base' is to be allocated. That's why it is the most derived class which is responsible for initializing all the subobjects of virtual base classes. 'XY' constructors initialize the 'Base' subobject and pointers to it in 'X' and 'Y'. After that, all the rest members of the classes 'X', 'Y' and 'XY' are initialized. Once the 'XY' constructor has initialized the 'Base' subobject, the 'X' and 'Y' constructors are not allowed to re-initialize it. The particular way it will be done depends on the compiler. For example, it can pass a special additional argument into the 'X' and 'Y' constructors to tell them not to initialize the 'Base' class. Now the most interesting thing which causes much confusion and a lot of mistakes. Have a look at the following constructors: X::X(int A) : Base(A) {} Y::Y(int A) : Base(A) {} XY::XY() : X(3), Y(6) {} What number will the base class's constructor take as an argument - 3 or 6? None! The constructor 'XY' initializes the virtual subobject 'Base' yet does that implicitly. It is the 'Base' constructor which is called by default. As the 'XY' constructor calls the 'X' or 'Y' constructor, it doesn't re-initialize 'Base'. That's why 'Base' is not being called with an argument passed into it. Troubles with virtual base classes don't end here. Besides constructors, there are also assignment operators. If I'm not mistaken, the standard tells us that an assignment operator generated by the compiler may assign values to a subobject of a virtual base class multiple times or once. So, you just don't know how many times the 'Base' object will be copied. If you implement your own assignment operator, make sure you have prevented multiple copying of the 'Base' object. The following code fragment is incorrect:
  • 3. XY &XY::operator =(const XY &src) { if (this != &src) { X::operator =(*this); Y::operator =(*this); .... } return *this; } This code leads to double copying of the 'Base' object. To avoid this, we should add special functions into the 'X' and 'Y' classes to prevent copying of the 'Base' class's members. The contents of the 'Base' class are copied just once, in the same code fragment. This is the fixed code: XY &XY::operator =(const XY &src) { if (this != &src) { Base::operator =(*this); X::PartialAssign(*this); Y::PartialAssign(*this); .... } return *this; } This code will work well, but it still doesn't look nice and clear. That's the reason why programmers are recommended to avoid multiple virtual inheritance. Virtual Base Classes and Type Conversion Because of the specifics of how virtual base classes are allocated in memory, you can't perform type conversions like this one: Base *b = Get(); XY *q = static_cast<XY *>(b); // Compilation error
  • 4. XY *w = (XY *)(b); // Compilation error A persistent programmer, though, will achieve that by employing the operator 'reinterpret_cast': XY *e = reinterpret_cast<XY *>(b); However, the result will hardly be of any use. The address of the beginning of the 'Base' object will be interpreted as a beginning of the 'XY' object, which is quite a different thing. See Figure 3 for details. The only way to perform a type conversion is to use the operator dynamic_cast. But using dynamic_cast too often makes the code smell. Figure 3. Type conversion. Should We Abandon Virtual Inheritance? I agree with many authors that one should avoid virtual inheritance by all means, as well as common multiple inheritance. Virtual inheritance causes troubles with object initialization and copying. Since it is the "most derived" class which is responsible for these operations, it has to be familiar with all the intimate details of the structure of base classes. Due to this, a more complex dependency appears between the classes, which complicates the project structure and forces you to make some additional revisions in all those classes during refactoring. All this leads to new bugs and makes the code less readable. Troubles with type conversions may also be a source of bugs. You can partly solve the issues by using the dynamic_cast operator. But it is too slow, and if you have to use it too often in your code, it means that your project's architecture is probably very poor. Project structure can be almost always implemented without multiple inheritance. After all, there are no such exotica in many other languages, and it doesn't prevent programmers writing code in these languages from developing large and complex projects. We cannot insist on total refusal of virtual inheritance: it may be useful and convenient at times. But always think twice before making a heap of complex classes. Growing a forest of small classes with shallow hierarchy is better than handling a few huge trees. For example, multiple inheritance can be in most cases replaced by object composition.
  • 5. Good Sides of Multiple Inheritance OK, we now understand and agree with the criticism of multiple virtual inheritance and multiple inheritance as such. But are there cases when it can be safe and convenient to use? Yes, I can name at least one: Mix-ins. If you don't know what it is, see the book "Enough Rope to Shoot Yourself in the Foot" [3] A mix-in class doesn't contain any data. All its functions are usually pure virtual. It has no constructor, and even when it has, it doesn't do anything. It means that no troubles will occur when creating or copying these classes. If a base class is a mix-in class, assignment is harmless. Even if an object is copied many times, it doesn't matter: the program will be free of it after compilation. References 1. Stephen C. Dewhurst. "C++ Gotchas: Avoiding Common Problems in Coding and Design". - Addison-Wesley Professional. - 352 pages; illustrations. ISBN-13: 978-0321125187. (See gotchas 45 and 53). 2. Wikipedia. Object composition. 3. Allen I. Holub. "Enough Rope to Shoot Yourself in the Foot". (You can easily find it on the Internet. Start reading at section 101 and further on).