Diamond inheritance (diamond problem) and diamond Inheritance Problem Diamond
When learning C ++, diamond inheritance is definitely an inevitable key issue. What is Diamond inheritance? Is it like a diamond? It's really ugly to draw pictures.
Because C ++ allows multi-inheritance, this will happen when the inheritance relationship is like this.
Class A is A base class, and Class B contains A, I represent B (A), and class C contains A, I represent C ()
Then there are B and C in D, which I represent as D (B (A) C ())
When we want to use A in D, or access A's part score, we can explain which A actually belongs to D, is A in d B A or A in C?
1 class A 2 {}; 3 class B: public A4 {}; 5 class C: public A6 {}; 7 class D: public B, public C8 {};
This can be an error. Some compilers won't even let you pass it and report an error directly to you.
This is embarrassing, isn't it? Even if the compilation phase passes you, don't try to access its internal A object through the D object like this, which will make the compiler very tangled
But it's easy to give a vitual.
1 class A 2 {}; 3 class B: virtual public A4 {}; 5 class C: virtual public A6 {}; 7 class D: public B, public C8 {};
He has his own A. when calling the variables or functions in A, he will call them in his own A, so that he will not tangle the compilation period.