Today read the Chenhao C + + virtual function table parsing of the article, feel the C + + inheritance and polymorphism have a little understanding, here write their own understanding. If something is wrong, please correct me. If you do not understand the C + + virtual function table, please first read the Chenhao of C + + virtual function table parsing article, or I may write you can not understand.
has always been for C + + polymorphic sense is very magical, from the book, Polymorphic is in the construction of sub-class objects, through the virtual function, the use of the parent class pointer, to invoke the real function of the child class. The explanation is correct, but how does it come about, and always guess. It was also known that there was a virtual function table and there was no careful understanding of what it was. Read Chenhao's article carefully today, only then understand the C + + polymorphic principle. Here's what I understand:
I'm going to attach a simple code that I wrote:
1#include <iostream>2 using namespacestd; 3 4 classbase{5 Public: 6 Virtual voidF () {cout <<"base::f ()"<<Endl;} 7 Virtual voidG () {cout <<"base::g ()"<<Endl;} 8 }; 9 classDerive: PublicBase {Ten Public: One Virtual voidF () {cout <<"devive::f ()"<<Endl;} A Virtual voidF1 () {cout <<"devive::f1 ()"<<Endl;} - Virtual voidG1 () {cout <<"devive::g1 ()"<<Endl;} - }; thetypedefvoid(*fun) (void); - - intMain () - { +Fun Pfun =NULL; - +Base B; Acout <<"Virtual table Address:"<< (int*) (&B) <<Endl; atcout <<"First virtual function address:"<< (int*)*(int*) (&B) <<Endl; -Pfun = (Fun) (* (int*)*(int*) (&b)); -Pfun (); -Pfun = (Fun) * ((int*)*(int*) (&B) +1); -Pfun (); - incout <<"*********"<<Endl; - toDerive D; +cout <<"Virtual table Address:"<< (int*) (&D) <<Endl; -cout <<"First virtual function address:"<< (int*)*(int*) (&D) <<Endl; thePfun = (Fun) (* (int*)*(int*) (&d)); *Pfun (); $Pfun = (Fun) * ((int*)*(int*) (&D) +1); Panax NotoginsengPfun (); -Pfun = (Fun) * ((int*)*(int*) (&D) +2); thePfun (); +Pfun = (Fun) * ((int*)*(int*) (&D) +3); APfun (); the
Output Result:
Virtual table Address: 0xbfd268f8
First virtual function address: 0x8048b90
Base::f ()
Base::g ()
*********
Virtual table Address: 0xbfd268f4
First virtual function address: 0x8048b78
Devive::f ()
Base::g ()
DEVIVE::F1 ()
DEVIVE::G1 ()
Understanding 1: First you want to implement polymorphism must pass the virtual function, if the 6th line we change to void F () {cout << "F" << Endl;}, then as the name implies this f () function will not go into the virtual function table, there is no polymorphism said
Understanding 2: Under 32-bit systems and 64-bit systems, the method of obtaining functions in the virtual function table is different, and then 32-bit system, as the program takes the line, and under the 64-bit system, the second function (fun) * ((int*) * (int*) (&d+2), the third function (fun ) * ((int*) * (int*) (&d+4) ....
Understanding 3: For the program manifest, we output the address of the v-table of class base and class derive respectively, and the address of the first virtual function, we find that their address is not the same, so that C + + will assign each Class A virtual table
Understanding 4: After understanding the structure of the virtual function table, I think that the polymorphic function call, is related to the virtual function table of the subclass, it can be said that the subclass is not directly related to (I thought the call of the polymorphic function is to go to the parent class function to find this function, and then in this subclass to find the same function call it, now 2 to the dregs of the rhythm)
With a few graphs to illustrate the call relationship
Note that the functions of the parent class A are virtual void f (), virtual void g (), virtual void H ()
The functions of subclass B are virtual void F1 (), virtual void G1 (), virtual void H1 ()
The functions of subclass C are virtual void F (), virtual void G1 (), virtual void H1 ()
Class A. When only the parent class
Class B. There are subclass inheritance, but no overloads for functions
Class C. There are subclass inheritance, there are function overloads
Such as: we call
Derive C;
Base *p_base = &c;
P_base->f ();
The calling procedure here is to find the address of the F () of the base class in the Class C virtual function table. The order of functions in the virtual function table is to put the virtual function of the parent class first, then the virtual functionof the subclass, and then get the address of the real letter that is stored at that address, which is the function of the subclass. So the function of the subclass is called
Copyright, if you want to reprint please indicate the source and author
I understand the C + + virtual function table