One , Multi level hybrid inheritance

Diamond inheritance is described below
// diamond inheritance class A { public: int data; }; class B:public A { public: int data; };
class C:public A { public: int data; }; class D:public B,public C { public: int
data; }; int main() { D c;; D.B::data=2;// visit B In D.C.::data=3;// visit C In
D.B::A::data=4;// visit B Inherited A D.C::A::data=5;// visit C Inherited A
D.A::data=4;// error , Produce ambiguity , I don't know B,C Which one of them }
*   Memory size of class sizeof(D); //20 sizeof(B); //8 sizeof(C); //8
* Memory diagram  
D Inherited from B To inherit again from C, therefore B The data of D Front most memory segment ,C Put it in B Behind ,D Put it at the end .

This diamond inheritance stores twice as much A Memory segment for , The following describes the virtual base class

Two , virtual base class (virtual)

1. concept : Also called virtual inheritance , diamond inheritance . When used for multi-level hybrid inheritance , Keep a virtual base class

2. Construction order

* Construct virtual base class first , If there are multiple virtual base classes , By declaration ( Left to right ) Sequential structure
* Reconstruct base class , If there are multiple base classes , By declaration ( Left to right ) Sequential structure
* If there are sub objects , Reconstruct sub pair image , If there are multiple sub objects , In order of declaration ( Top to bottom ) Sequential structure
* Finally, construct yourself class A // virtual base class { public: A(int data){}//1 int data; }; class
B:virtual public A { public: B():A(1){}//2 int data; }; class C:virtual public
A { public: C():A(2){}//3 int data; }; class D:public B,public C { public:
D():A(3){}//4 int data; }; int main() { D d;; d.B::data=2;
d.C::data=3;; d.C::A::data=5; }
* The order of construction is :1-2-3-4
Structural sequence interpretation : Construction class D object d When , Discovery inherited from B, So to construct B, structure B When , Found inherited from virtual base class A, So we construct the virtual base class A, Then construct B. And then construct C, find C Inherit from virtual base class A, But virtual base class found A Has been B Constructed , So it's no longer structured A, Direct structure C. Final construction D
* If the virtual base class constructor is a construction with parameters , Then its subclass , And subclasses extended from subclasses , All of them are initialized with constructors in the member initialization list
* d Of data Assigned as 1, Inherited from B,C, Assigned as 2,3, From left to right .A The value of is 4, Later, when the last line is executed, it is assigned as 5
3. Memory size of class

* Classes inherited from virtual base classes , Memory address size plus 4 byte ( this 4 Bytes belong to the virtual base class , It's not your own ) sizeof(B); //12 sizeof(C); //12
sizeof(D); //24
4. Memory address diagram

* The memory address of the virtual base class is at the end of the memory address of the derived class
* When a virtual base class is derived , Save only one copy of the existing derived class memory

  Address resolution :

* B and C All saved in A Value of , But in D inherit B and C When , Only one copy has been saved A, And put it at the end
* stay D Inherited B and C There is a function pointer in the memory segment at the front

©2019-2020 Toolsou All rights reserved,
Python+OpenCV Detailed explanation of face recognition technology SpringBoot practice ( five ):mybatis-plus In BaseMapper,Iservice and ServiceImpl Google says home office affects work efficiency !2021 Return to offline office in 2010 C Language programming to find a student's grade vue Of v-if And v-show The difference between C Language console games , Make bricks These songs , Programmers, don't listen ! What should I do if I suddenly encounter a question I can't answer during the interview ?python To solve the problem of dictionary writing list in c Linguistic 5 Three common sorting methods