what are the disadvantage of multiple inheritance?

The major problem that one must watch out for when using multiple inheritance is if more than one of the base classes are derived and one of their base classes is the same. Unless you use virtual inheritance, then you get two instances of that base-base class in each instance of the derived class, so if you make a call that is handled by that base-base class, which instance handles it may be unknown, and their state data may vary. Virtual inheritance helps assure that there is only one instance of that base-base class.

Used carefully, multiple inheritance is incredibly useful.

As rubberman says, the possibility of repeated base-classes (which can be solved with virtual inheritance) is one problem. Another problem is with other ambiguities between inherited classes, like data members or member functions with the same name in the different classes (even if those members are private, the ambiguity will still be there).

Overall, the problem is that once you allow the possibility of any class being used in a multiple inheritance scheme, you have to worry about all sorts of bad interactions like the ones just mentioned. This breaks encapsulation, in the sense of having self-contained classes, and can quickly become difficult to deal with.

Also, once you allow multiple inheritance, you also have to understand that up and down casts between base and derived classes could potentially lead to (virtual) offsets being applied to the pointers (or references), which will make the use of C++ casts (as opposed to C-style casts) very important.

In other words, using multiple inheritance in a wide-spread manner requires a lot more care and diligence throughout the code, everywhere. That's why it's often discouraged (and I would discourage it too).

That said, there are tricks and specific use-cases where multiple inheritance can be a very elegant and beneficial feature. As long as it remains an occasional trick that you resort to for specific and limited purposes, it's perfectly fine.

The disadvantage of multiple inheritance is that for using it you need to know more than for not using it. However, if you know how to use it, it is a fantastic tool.

In other words, knowing a little of multiple inheritance, you can make a mess; knowing a lot, you can make very powerful things.

One important thing to take into account is that for doing any software you don't need Multiple Inheritance; in the same way you don't need inheritance at all; even you don't need data abstraction; nor the structures nor classes; and so on. At the end probably you only need a basic assembler. Remember that the humans went to the moon without neither computers nor calculators. In this way of think, you can not learn how to use Multiple Inheritance at all and probably nothing will happen while you don't try to use it. On the contrary, you can learn it very well and you will enjoy constructing interesting software.

There are programming languages that were constructed with the idea that Multiple Inheritance is dangerous (or difficult to learn) and have suppressed it. I think that all the languages are dangerous and difficult to learn on some degree; so probably, with the idea of those people, the most secure thing to do should be to restrict the use of languages to only very clever people.

C++ has included Multiple Inheritance with a very powerful concept, if you don't use it, you don't pay for it. That's if you don't like it, just forget it and then, there are no disadvantage of multiple inheritance at all.

With all of this, you can say that Multiple Inheritance in C++ has no disadvantage at all, but can have many advantages.

You asked for an example!

Consider you have the classes Widget and SpecialWidget using simply inheritance like:

class Widget
{
   /*...*/
};

and

class SpecialWidget: public Widget
{
   /*...*/
};

Suppose that you need to count the number of objects that exist of class SpecialWidget at some point in your program.

Consider you have the following counter.h file (It is another problem to explain why it is written like this):

template<typename T>
class Counter
{
public:
  Counter() {++count;}
  ~Counter() {--count;}

  static size_t howMany() {return count;}

private:
  static size_t count;
};

template<typename T>
size_t Counter<T>::count = 0;

Well, to count the numbers of objects created of class SpecialWidget you only need to include counter.h into your widget.h file and modify your code in the following way:

class SpecialWidget: public Widget, private Counter<SpecialWidget>
{
   /*...*/
};

When you want to know the number of objects of SpecialWidget that exist at some point, at that point you just write:

int numberOfSpecialWidgetObjects = Counter<SpecialWidget>::howMany();

You can do exactly the same for any other class you need to count the number of objects:

class Derived : public Base, private Counter<Derived>
{
   /*...*/
};

and know the number of objects that exist at some point of Derived class with:

int numberOfDerivedObjects = Counter<Derived>::howMany();

Doing this without Multiple Inheritance is possible but not as elegant as it is with Multiple Inheritance.

Note: I didn't test the written code, so probably there are some pitfalls. I apologize in advance.

Edited 1 Year Ago by Maritimo

This article has been dead for over six months. Start a new discussion instead.