Example Analysis of Diamond inheritance in C++
This article mainly shows you the "example analysis of diamond inheritance in C++", which is easy to understand and well-organized. I hope it can help you solve your doubts. Now let the editor lead you to study and study the "example analysis of diamond inheritance in C++".
Diamond inheritance
Class Person {int _ AA;}; class Student:public Person {int _ BB;}; class Teacher: public Person {int _ CC;}; class Assistant: public Student, public Teacher {int _ DD;}
PS:
There are two Person members in the object of Assistant
Diamond inheritance has ambiguity and data redundancy.
Resolve:
Use virtual inheritance
First of all, when virtual inheritance is not used:
# include using namespace std; class AA {public: string _ aa;}; class BB: public AA {public: int _ bb;}; class CC: public AA {public: int _ cc;}; class DD: public BB, public CC {public: int _ dd;}; int main () {DD d; d.BB::_aa = 1; d.CC::_aa = 2; d._bb = 3; d._cc = 4; d._dd = 5; cout