Virtual function inheritance and virtual inheritance are two completely different concepts .

Virtual function inheritance solves polymorphism , When you point to a derived class object with a base class pointer , When the base class pointer calls the virtual function, it will automatically call the virtual function of the derived class , This is polymorphism , Also called dynamic editing .

Virtual inheritance is to save memory , He is a unique concept in multiple inheritance . Applicable to diamond inheritance form .

such as B Inherited from A,C Inherited from A,D Inherited from B and C, obviously D Will inherit twice A( chart 1). therefore , To save space , You can B,C Yes A Inheritance of is defined as virtual inheritance , and A It becomes a virtual base class ( chart 2). The code is as follows :

 A    A           A
  \    /           / \
  B   C           B  C
   \  /            \  /
   D             D
  ( chart 1)          ( chart 2)
class A; class B:vitual public A; class C:vitual public A; class D:public B,
public C;
In virtual inheritance, a subclass will have a pointer to its virtual function table , A virtual class pointer to the parent class will also be added , Then you need to include all the contents of the parent class .

In virtual inheritance, if the parent class of a subclass has virtual functions , Then it will recreate a virtual table , Does not contain the contents of the parent virtual table ; In common inheritance, a virtual table is created based on the virtual table of the parent class . This means that if the subclass parent class of virtual inheritance has its own virtual function , There will be two virtual function table pointers in the subclass , A virtual table pointing to the parent class , A virtual table pointing to a subclass , But ordinary inheritance has only one pointer to the virtual table of the subclass . Code description :
class A { int k; public: virtual void aa(){}; }; class B:public virtual A { int
j;public: virtual void bb(){}; }; class C:public virtual B { int i; public:
virtual void cc(){}; }; int main() { cout << sizeof(A) << endl; cout << sizeof
(B) << endl; cout <<sizeof(C) << endl; system("pause"); return 0; }
The output is :8,20,32.

How come ? class A Contains an integer variable in k(4 byte ), A virtual table pointer (4 byte ), So there are 8 byte . class B in , An integer variable j(4 byte ), A virtual table pointer (4 byte ), because B Virtual inheritance in A, All will have a pointing class A Virtual class pointer of (4 byte ), Also include classes A Integer variable in k(4 byte ) And classes A Virtual table pointer of (4 byte ), So there are 20 byte . class C Homology .

If you change the above code to normal inheritance , Then the output is :8,12,16. No virtual class pointer , There will be no more than one virtual table pointer .

©2019-2020 Toolsou All rights reserved,
First knowledge python Skills summary GDOI2019 travels c Linguistic 5 Three common sorting methods Python Basic knowledge and notes " Cephalosporin wine Say go and go "? DANGER ! Don't drink alcohol when taking these drugs Thorough explanation from Zhongtai vue The component page height is adaptive according to the screen size Classical algorithm - recursion ( The case of raw rabbit ) Big data tells you , How tired are Chinese women about keras use fit_generator Encountered in StopIteration