
Anonymous A. answered 03/01/22
Professional Game Developer Specializing in C++
Hello,
Virtual base classes are a tool to prevent the same class being inherited multiple times when using multiple inheritance. This is typically demonstrated in the diamond problem. Consider the following classes:
The class hierarchy would look like this:
A
/ \
B C
\ /
D
hence, the title the diamond problem.
In the above code, instantiations of class D actually have two versions of the member variable x, and this code will result in a compile error due to ambiguity.
Virtual inheritance will fix the ambiguity error. The code would then look like this:
}