First, we understand what is single inheritance, what is multiple inheritance??
Single inheritance: A subclass has only one direct parent class.
Multiple inheritance: A subclass has two or more direct parent classes.
virtual function Table Analysis in single inheritance:
Sample program:
#include <iostream>using namespace std;typedef void (*func) (); Class base{public: VIRTUAL&NBSP;VOID&NBSP;FUNC1 () {cout << "base::func1 ()" &NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC2 () {cout << "Base::func2 ()" &NBSP;<<&NBSP;ENDL;} private:int _b;}; class derive :p ublic base{public:virtual void func1 () {cout << "Derive :: Func1 () "&NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC3 () {cout << "derive::func3 ()" &NBSP;<<&NBSP;ENDL;} Virtual void func4 () {cout << "Derive::func4 ()" &NBSP;<<&NBSP;ENDL;} private:int _d;}; Void printvfptr (INT*&NBSP;VPTR)//Print virtual function table {cout << "virtual function table: " << vptr << endl;for (int i = 0; vptr[i] != 0; ++i) {printf ("% D A virtual function:%p >> ", i, vptr[i]); func f = (FUNC) (vptr[i]); F ();}} Void test () {base b;derive d;int* vptrbase = (int*) (* (int*) (&b));int* vptrderi = (int*) (* (int*) (&d)); Printvfptr (vptrbase);cout << endl; Printvfptr (Vptrderi);} Int main () {Test (); System ("pause"); return 0;}
Program Run Result:
virtual function table for base class (base class):
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7C/EA/wKioL1bcEMbyJxs-AAAQf1oQ-Ns235.png "title=" _8} 9awz_uix9tfcowghmy ' B.png "alt=" Wkiol1bcembyjxs-aaaqf1oq-ns235.png "/>
Virtual function table for the derive class (derived class):
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7C/EB/wKiom1bcEHuxOB0QAAAcF7C983I617.png "title=" J ' QU} y2xyw[r3och0l ' J) [m.png "alt=" Wkiom1bcehuxob0qaaacf7c983i617.png "/>
Conclusion:
If there is a virtual function table, then there is only one virtual function table, and in the order in which the virtual function is declared, the virtual function of the derived class is immediately followed by the virtual function of the base class . .
Analysis of virtual function table in multiple inheritance:
Example code:
#include <iostream>using namespace std;typedef void (*func) (); Class base{public: VIRTUAL&NBSP;VOID&NBSP;FUNC1 () = 0;virtual void func2 () = 0;}; CLASS&NBSP;BASE1:PUBLIC&NBSP;BASE{PUBLIC:VIRTUAL&NBSP;VOID&NBSP;FUNC1 () {cout << "Base1::func1 ( ) "&NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC2 () {cout << "Base1::func2 ()" &NBSP;<<&NBSP;ENDL;} private:int _b1;}; CLASS&NBSP;BASE2:&NBSP;PUBLIC&NBSP;BASE{PUBLIC:VIRTUAL&NBSP;VOID&NBSP;FUNC1 () {cout << "BASE2:: Func1 () "&NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC2 () {cout << "Base2::func2 ()" &NBSP;<<&NBSP;ENDL;} private:int _b2;}; Class derive : public base1, public base2{public:virtual void func1 () { cout << "Derive::func1 ()" &NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC3 () {cout << "derive::func3 ()" &NBSP;<<&NBSP;ENDL;} VIrtual void func4 () {cout << "Derive::func4 ()" &NBSP;<<&NBSP;ENDL;} private:int _d;}; Void printvfptr (base* b) {int* vtable1 = (int*) (* (int*) b);cout << " Virtual function table pointer: " << vTable1 << endl;for (int i = 0; vtable1[i ] != 0; ++i) {printf ("%d virtual function pointer: %p >>", i, vtable1[i]); func f = (FUNC) vtable1[i];f ();} int* vtable2 = (int*) (* (int*) b + sizeof (BASE1) / 4); int* vtable2 = (int*) (* ((int*) ((char*) b + sizeof (BASE1)));cout << "virtual function table pointer:" < < vTable2 << endl;for (int i = 0; vtable2[i] != 0; ++i) {printf ("%d virtual function pointer: %p >>", i, vtable2[i]); func f = (FUNC) vtable2[i];f ();}} Void test () {base1 b1; base2 b2;derive d; Printvfptr (&B1);cout << endl; Printvfptr (&B2);cout << endl; Printvfptr ((base1*) &d);} Int main () {Test (); System ("pause"); return 0;}
Program Run Result:
virtual function table for BASE1:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7C/EA/wKioL1bcFcSzAXRSAAAX-leGeyg336.png "title=" pi81y} Zxjyg (ljv1b7m}y%e.png "alt=" Wkiol1bcfcszaxrsaaax-legeyg336.png "/>
virtual function table for Base2:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7C/EA/wKioL1bcFq2xTaMeAAAQJsD_jQU490.png "title=") $}1s] 2VBT] @UK%aau4w1s9.png "alt=" Wkiol1bcfq2xtameaaaqjsd_jqu490.png "/>
Virtual function table for derive: (with two virtual function tables)
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7C/EA/wKioL1bcFyCAR3u_AAAq7ZbMpMs067.png "title=" NWVQV0 ) x%f_bd4yuq9t2@~4.png "alt=" Wkiol1bcfycar3u_aaaq7zbmpms067.png "/>
Diamond inheritance in multiple inheritance:
Example code:
#include <iostream>using namespace std;typedef void (*func) (); Class base{public: VIRTUAL&NBSP;VOID&NBSP;FUNC1 () {cout << "base::func1 ()" &NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC2 () {cout << "Base::func2 ()" &NBSP;<<&NBSP;ENDL;} private:int _b;}; class parent1 :p ublic base{public:virtual void func1 () {cout << " PARENT1::FUNC1 () "&NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC3 () {cout << "parent1::func3 ()" &NBSP;<<&NBSP;ENDL;} Virtual void func4 () {cout << "Parent1::func4 ()" &NBSP;<<&NBSP;ENDL;} Virtual void func5 () {cout << "Parent1::func5 ()" &NBSP;<<&NBSP;ENDL;} private:int _p1;}; class parent2 :p ublic base{public:virtual void func1 () {cout << " PARENT2::FUNC1 () "&NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC3 () {cout << "pareNt2::func3 () "&NBSP;<<&NBSP;ENDL;} Virtual void func6 () {cout << "Parent2::func6 ()" &NBSP;<<&NBSP;ENDL;} Virtual void func7 () {cout << "Parent2::func7 ()" &NBSP;<<&NBSP;ENDL;} private:int _p2;}; class child :p ublic parent1, public parent2{public:virtual void func1 () { cout << "Child::func1 ()" &NBSP;<<&NBSP;ENDL;} VIRTUAL&NBSP;VOID&NBSP;FUNC3 () {cout << "child::func3 ()" &NBSP;<<&NBSP;ENDL;} Virtual void func4 () {cout << "Child::func4 ()" &NBSP;<<&NBSP;ENDL;} Virtual void func6 () {cout << "Child::func6 ()" &NBSP;<<&NBSP;ENDL;} Virtual void func8 () {cout << "Child::func8 ()" &NBSP;<<&NBSP;ENDL;} private:int _c;}; Void printvfptr (INT*&NBSP;VPTR)//Print virtual function table {cout << "virtual function table: " << vptr << endl;for (int i = 0; vptr[i] != 0; ++i) {printf ("%d virtual function:%p >> ", i, Vptr[i]); func f = (FUNC) (vptr[i]); F ();}} Void test () {base b; parent1 p1; parent2 p2; child c;int* vptr_b = (int*) (* (((int*) (&b)));int* vptr_p1 = (int*) (* ( (int*) (&P1))); int* vptr_p2= (int*) (* (((int*) (&P2))); Printvfptr (Vptr_b);cout << endl; Printvfptr (VPTR_P1);cout << endl; Printvfptr (VPTR_P2);cout << endl;int* vptr_c_1 = (int*) (* ((int*) (&c))); int* vptr_c_2 = (int*) (* (int*) ((char*) (&c) + sizeof (Parent1))); Printvfptr (vptr_c_1);cout << endl; Printvfptr (vptr_c_2); Cout << endl;} Int main () {Test (); System ("pause"); return 0;}
Operation Result:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7C/EA/wKioL1bcGALwhph7AABvr4tJVO0897.png "title=" virtual function table. PNG "alt=" Wkiol1bcgalwhph7aabvr4tjvo0897.png "/> Where child has two virtual function tables because it has two direct parent classes.
Summary: There will be multiple virtual function tables in multiple inheritance, and several virtual function tables will be available for several inheritance. These virtual function tables are arranged sequentially in the order in which they are derived. If the subclass overrides the virtual function of the parent class, the corresponding parent virtual function is overwritten with the subclass's own virtual function, and if the subclass has a new virtual function, it is added to the end of the first virtual function table.
Virtual function tables and virtual function pointers in single-inheritance and multiple-inheritance