Search code examples
c++inheritancecompiler-constructionmultiple-inheritancelanguage-implementation

How is C++'s multiple inheritance implemented?


Single inheritance is easy to implement. For example, in C, the inheritance can be simulated as:

struct Base { int a; }
struct Descendant { Base parent; int b; }

But with multiple inheritance, the compiler has to arrange multiple parents inside newly constructed class. How is it done?

The problem I see arising is: should the parents be arranged in AB or BA, or maybe even other way? And then, if I do a cast:

SecondBase * base = (SecondBase *) &object_with_base1_and_base2_parents;

The compiler must consider whether to alter or not the original pointer. Similar tricky things are required with virtuals.


Solution

  • The following paper from the creator of C++ describes a possible implementation of multiple inheritance:

    Multiple Inheritance for C++ - Bjarne Stroustrup