I've heard multiple inheritance gives compilers grief?

Jan 11, 2012 at 4:39am
I've heard that the reason that Java and many other OOP languages don't support C++ style Multiple Inheritance is because of having to deal with such things in memory. I would like to know what people actually mean? What difficulties do compilers face when trying to cope with MI?
Jan 11, 2012 at 5:01am
During the developement of C++, Stroustrup as told that multiple inheritance could not be implemented efficiently. So he provided an efficient implementation in the first draft of C++ ahead of templates and exceptions. And the feature has defined C++ ever since.

The problem with multiple inheritance is not memory or machine issue, it's people. What does it mean if a base class appears in multple time in the inheritance tree?

It turns out that supporting multiple interfaces is not the problem, it's the conflation of rules on virtual functions, overloaded functions, order of construction ... that is the problem. The solution these days is to multply inherit interface and manually do the implementation.
Jan 11, 2012 at 5:03am
Class layout becomes complicated: take a look at Watcom's, for example: http://tiny.cc/rbkux -- note that it includes "thunks" (pieces of executable code) to be called for something as trivial as pointer casts.

Member function pointers (aka delegates in other languages) also become complicated, and also turn into pieces of executable code instead of just addresses: http://www.codeproject.com/KB/cpp/FastDelegate.aspx

But really, I think it's more of a language design decision than implementation difficulty. There are successful compilers for much more complex languages than C++.
Last edited on Jan 11, 2012 at 5:06am
Jan 11, 2012 at 11:49am
Does it make sense to adopt a Java-esque approach?

i.e. only allow const static data and pure virtual functions in base classes that might be used in multiple inheritance

In this case would you still need to use virtual inheritance?
Jan 11, 2012 at 11:54am
only allow const static data and pure virtual functions in base classes that might be used in multiple inheritance


I use pure virtual functions to create abstract base classes. I'd be upset if this was no longer an option because I didn't intend to do any multiple inheritance.
Jan 11, 2012 at 1:12pm
You don't really need virtual inheritance. It's an artifact of multiple inheritance (of implementation), which you don't need either.

However, virtual inheritance is used in the odd pattern, but not for its inheritance properties, but for the order of construction and cast properties.

The Java solution, single inheritance of a class and multiple inheritance of interface, is a nice enough pattern that works.

COM provides interfaces only, with a factory to create the object.

There are a number of althernatives.
Jan 11, 2012 at 1:24pm

The solution these days is to multply inherit interface and manually do the implementation.


No, this is Java and C# solution. The state-of-the-art is to multiply inherit interface, methods and data (C++ got it), but once and only once (like inheriting interfaces), in a well defined order so that construction order / overriding / super references etc. are all well defined and easy to analyze. This is currently implemented in Ruby, Python and Scala as far as I know.
Last edited on Jan 11, 2012 at 1:25pm
Topic archived. No new replies allowed.