C + + virtual function table

Source: Internet
Author: User

You know that virtual functions are implemented by a virtual function table. In this table, is mainly a class of virtual function of the Address table, this table solves the problem of inheritance, overwrite, its content is really the function of the actual response. Thus, in an instance of a class with a virtual function, the table is allocated to the memory of the instance, so the virtual function table is especially important when manipulating a subclass with a pointer to the parent class. It is like a map, indicating the function that should actually be called.

In the Standard Rules of C + +, the compiler must ensure that a pointer to a virtual function table exists in the first position in the object instance (this is to ensure that the offset of the virtual function is correctly taken). This means that the virtual function table is obtained through the address of the object instance, and then the function pointer can be traversed and the corresponding function is called.

#include <iostream>using namespacestd;classbase{ Public:    Virtual voidFUN1 () {cout<<"base::fun1\n";} Virtual voidFun2 () {cout<<"base::fun2\n";} Virtual voidFUN3 () {cout<<"base::fun3\n";}Private:    intNUM1; intnum2;}; typedefvoid(*fun) (void);intMain () {Base B;    Fun Pfun; //3 Virtual Functions of object B were called by pointers. Pfun = (Fun) * ((int*)*(int*) (&AMP;B) +0 );    Pfun (); Pfun= (Fun) * ((int*)*(int*) (&AMP;B) +1 );    Pfun (); Pfun= (Fun) * ((int*)*(int*) (&AMP;B) +2 );    Pfun (); return 0;}/*The program execution results are as follows: Base::fun1base::fun2base::fun3press <RETURN> to close the This window ...*/

The base object B memory structure in the program is as follows:

How many virtual function tables does a class have?

For a single-inherited class, if it has a virtual function, there is only one virtual function table. For a multiple-inheritance class, it may have more than one table of virtual functions.

#include <iostream>using namespacestd;classbase1{ Public: Base1 (intnum): num_1 (num) {}Virtual voidFUN1 () {cout<<"base1::fun1"<<num_1<<Endl;} Virtual voidFun2 () {cout<<"base1::fun2"<<num_1<<Endl;} Virtual voidFUN3 () {cout<<"Base1::fun3"<<num_1<<Endl;}Private:    intnum_1;};classbase2{ Public: Base2 (intnum): num_2 (num) {}Virtual voidFUN1 () {cout<<"base2::fun1"<<num_2<<Endl;} Virtual voidFun2 () {cout<<"base2::fun2"<<num_2<<Endl;} Virtual voidFUN3 () {cout<<"Base2::fun3"<<num_2<<Endl;}Private:    intnum_2;};classbase3{ Public: Base3 (intnum): num_3 (num) {}Virtual voidFUN1 () {cout<<"base3::fun1"<<num_3<<Endl;} Virtual voidFun2 () {cout<<"base3::fun2"<<num_3<<Endl;} Virtual voidFUN3 () {cout<<"Base3::fun3"<<num_3<<Endl;}Private:    intnum_3;};classDerived1: Publicbase1{ Public: Derived1 (intnum): Base1 (num) {}Virtual voidFder1_1 () {cout<<"derived1::fder1_1\n";}//no coverage    Virtual voidFder1_2 () {cout<<"derived1::fder1_2\n";}};classDERIVED2: Publicbase1{ Public: Derived2 (intnum): Base1 (num) {}Virtual voidFun2 () {cout<<"derived2::fun2"<<endl;}//only covers the base1::fun2    Virtual voidFder2_1 () {cout<<"derived2::fder2_1\n";} Virtual voidFder2_2 () {cout<<"derived2::fder2_2\n";}};classDERIVED3: PublicBASE1, PublicBase2, PublicBase3//multiple inheritance, no overwrite{ Public: Derived3 (intNum_1,intNum_2,intnum_3): Base1 (num_1), Base2 (num_2), Base3 (num_3) {}Virtual voidFder3_1 () {cout<<"derived3::fder3_1\n";} Virtual voidFder3_2 () {cout<<"derived3::fder3_2\n";}};classDERIVED4: PublicBASE1, PublicBase2, PublicBase3//multiple inheritance, with overrides{ Public: Derived4 (intNum_1,intNum_2,intnum_3): Base1 (num_1), Base2 (num_2), Base3 (num_3) {}Virtual voidFUN1 () {cout<<"derived4::fun1\n";}//fun1 functions that cover all base classes    Virtual voidFder4_1 () {cout<<"derived4::fder4_1\n";}};intMain () {Base1*pbase1 =NULL; Base2*pbase2 =NULL; BASE3*PBASE3 =NULL; cout<<"-----generally inherited from Base1, no cover------\ n"; Derived1 D1 (1); PBase1= &D1; PBase1-fun1 (); cout<<"-----generally inherited from Base1, covering fun2---\ n"; Derived2 D2 (2); PBase1= &D2; PBase1-fun2 (); cout<<"-----Multiple inheritance, no cover-----------------\ n"; DERIVED3 D3 (1,2,3); PBase1= &D3; PBase2= &D3; PBASE3= &D3; PBase1-fun1 (); PBase2-fun1 (); PBASE3-fun1 (); cout<<"-----Multiple inheritance, covering fun1-------------\ n"; Derived4 D4 (1,2,3); PBase1= &D4; PBase2= &D4; PBASE3= &D4; PBase1-fun1 (); PBase2-fun1 (); PBASE3-fun1 (); return 0;}/** The results of the program operation are as follows:-----generally inherited from Base1, no cover------BASE1::FUN1 1-----Generally inherited from Base1, CO Vering fun2---derived2::fun2-----multiple inheritance, no cover-----------------base1::fun1 1base2::fun1 2base3:: FUN1 3-----Multiple inheritance, covering fun1-------------derived4::fun1derived4::fun1derived4::fun1press < Return> to close the This window ...*/

General Inheritance (no virtual function overrides)

The Derived1 class inherits from the Base1 class, without any functions overriding the base class, so that the two virtual functions of Dervied1 are added sequentially to the end of the virtual function table. The virtual function table for Derived1 is as follows:

General inheritance (overrides with virtual functions)

Derived2 inherits from the Base1 class and overrides the Fun2 () in the base class. Therefore, the derived2::fun2 in the virtual function table replaces the BASE::FUN2, when a new virtual function in the derived class is added to the footer of the virtual function. The virtual function table for Derived2 is as follows:

Multiple inheritance (no virtual function overrides)

Derived3 inherits from Base1,base2,base3, and its virtual function table is as follows:

Each parent class of DERIVED3 has its own virtual table, so DERIVED3 has 3 virtual tables. The order of the parent virtual tables here is consistent with the order in which the declaration inherits the parent class. This is done in order to resolve pointers to different parent class types to the same child class instance, and to invoke the actual function. For example:

New Derived3 ();p base->fun2 ();

If you point a pointer to the BASE2 type to the DERIVED3 instance, the call will be those functions corresponding to the BASE2 virtual table.

Multiple inheritance (with virtual function overrides)

The Derived4 class inherits from Base1,base2,base3 and overrides the FUN1 function of the 3 base classes. The virtual function table is as follows:

You can see that the fun1 in the base class are replaced with derived4::fun1 so that we can point the parent class of any static type to the subclass and call F () of the subclass.

New Derived4 ();p Base1->fun1 ();

C + + virtual function table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.