Jump to content

  • Log In with Google      Sign In   
  • Create Account
Mark as peer reviewed: 0 votes
Still needs work: 0 votes


Wade Not in Unknown Waters: Part Four UNDER REVIEW

By Andrey Karpov | Published Jul 24 2013 11:44 AM in General Programming

c++ cpp


Initialization of Virtual Base Classes


class Base { ... };
class X : public Base { ... };
class Y : public Base { ... };

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 sub-objects. 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 { ... };

Figure 2. Multiple virtual inheritance.

It is at the end of the XY object that memory for the shared sub-object 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 sub-object is rather implemented through offset or as information stored in the virtual function table.

The "most derived" class XY alone knows where exactly a sub-object 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 sub-objects of virtual base classes.

XY constructors initialize the Base sub-object and pointers to it in X and Y. After that, all the remaining members of the classes X, Y and XY are initialized.

Once the XY constructor has initialized the Base sub-object, 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.




The constructor XY initializes the virtual sub-object 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 sub-object 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:

  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:

  if (this != &src)
    Base::operator =(*this);
  return *this;


Virtual Base Classes and Type Conversion


Base *b = Get();
XY *q = static_cast<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.


Figure 3. Type conversion.

Should We Abandon Virtual Inheritance?



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.


Good Sides of Multiple Inheritance






  2. Wikipedia. Object composition.


GDOL (Gamedev.net Open License)


I believe by mix-in you are referring to a pure abstract interface. As far as my understanding goes, a mix-in is a class that provides a certain set of methods or properties to a given object. So in a sense all mix-ins are interfaces, but not all interfaces are mix-ins.

Note: GameDev.net moderates article comments.