Coming from a java background I have never encountered the diamond problem where multiple inheritance can cause overriding problems as described in detail at http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem
But if this problem occurs will the c++ runtime complain or is it random which implementation of the super class method will be invoked ?
I’ve read the ‘Mitigation’ part of article but did’nt fully understand it.
The compiler will catch a program encountering the diamond problem by diagnosing any ambiguity.
One solution is to remove the ambiguity. This can be done when referring to members by using explicit name qualification:
Or if you want to access a base sub-object as in:
B *b = new E;It’s ambiguous whether you want the base sub-object from D1 or D2. Using an explicit cast to one of those intermediate types resolves the ambiguity.Also note that down-casting from a B* to an E* is not possible to do statically; the compiler doesn’t know which B is being pointed to so it doesn’t statically know how to adjust the pointer to get back to E. This is where dynamic_cast becomes necessary.
Another solution is to sidestep the issue with virtual inheritance, thus avoiding multiple base sub-objects of the same type.