To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at

If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

Wade Not in Unknown Waters. Part Four

Wade Not in Unknown Waters. Part Four

Jul 15 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);
  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.


  • 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).
  • Wikipedia. Object composition.
  • 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).
Popular related articles
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →