Hi There!

I'm Dan Schlegel, an Associate Professor in the Computer Science Department at SUNY Oswego

Diamond Problem

The “Deadly Diamond of Death”

Consider a set of classes with the following inheritance hierarchy.

diamond.cpp

Outputs:

Our class hierarchy ended up looking more like this:

This is because C++ by default uses replicated inheritance.

Let’s investigate further. Now add printValue() to D’s main:

Output:

The same thing will happen if we try to add

to the main. We could try to tell the compiler which A we mean by saying something like:

which works, but doesn’t solve the problem that we have two different A’s.

Solution: use shared multiple inheritance – make B and C inherit from virtual A.

The actual machinery behind all of this is quite complex, but you can read about it a bit here.