What's happening behind the scene.
The simple explanation are, because inheritance from are Base virtual in both Der1 Der2 and, there are a single Insta nCE of the object in the most derived object Join . At compile time, and assuming (which was the common case) virtual tables as dispatch mechanism, when compiling Der1::foo it wil L REDIRECT the call to bar() through the vtable. Now the question are how the compiler generates vtables for each of the objects and the vtable for would Base contain both null Pointers, the vtable for would Der1 contain and Der1::foo a null pointer and the vtable for would Der2 contain a null point ER and Der2::bar [*] Now, because of virtual inheritance at the previous level, when the compiler processes Join it would create a Base single object, and thus a single vtable for the Base subojbect of Join . It effectively merges the vtables of and and Der1 Der2 produces a vtable that contains pointers to Der1::foo and Der2::bar . So the code in would Der1::foo dispatch through Join ' s vtable to the final Overrider, which in this case was in a different B Ranch of the virtual inheritance hierarchy. If you add a Der3 class, and that class defines either of the virtual functions, the compiler is not being able to cleanly Merge the three vtables and would complain, with some error relating to the ambiguity of the multiply defined method (none Of the overriders can is considered to be the final Overrider). If you add the same method Join to and then the ambiguity'll no longer be a problem, as the final overrider'll be the Me mber function defined Join in, so the compiler was able to generate the virtual table. [*] Most compilers won't write null pointers here, but rather a pointer to a generic function that'll print an error mess Age terminate and the application, allowing for better diagnostics than a plain segmentation fault. |