C ++ virtual function table, function table

Source: Internet
Author: User

C ++ virtual function table, function table

We all know that virtual functions are implemented through a virtual function table. In this table, it is mainly an address table of class virtual functions. This table solves the inheritance and overwrite issues, and its content truly reflects the actual functions. In this way, the table is allocated to the memory of the instance in instances of classes with virtual functions. Therefore, when the parent class pointer is used to operate a subclass, this virtual function table is particularly important. Like a map, it specifies the function to be called.

According to the standard rules of C ++, the compiler must ensure that the pointer of the virtual function table exists in the front of the object instance (to ensure that the offset of the virtual function is obtained correctly ). This means that the virtual function table is obtained through the address of the object instance, and then function pointers can be traversed and corresponding functions can be called.

 

# Include <iostream> using namespace std; class Base {public: virtual void fun1 () {cout <"Base: fun1 \ n" ;}virtual void fun2 () {cout <"Base: fun2 \ n";} virtual void fun3 () {cout <"Base: fun3 \ n";} private: int num1; int num2 ;}; typedef void (* Fun) (void); int main () {Base B; Fun pFun; // three virtual functions of object B are called through pointers. PFun = (Fun) * (int *) (& B) + 0); pFun (); pFun = (Fun) * (int *) * (int *) (& B) + 1); pFun (); pFun = (Fun) * (int *) (& B) + 2); pFun (); return 0;}/* The execution result of the program is as follows: Base: fun1Base: fun2Base: fun3Press <RETURN> to close this window... */

The memory structure of Base object B 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 multi-inherited class, it may have tables with multiple virtual functions.

# Include <iostream> using namespace std; class Base1 {public: Base1 (int num): num_1 (num) {} virtual void fun1 () {cout <"Base1 :: fun1 "<num_1 <endl;} virtual void fun2 () {cout <" Base1: fun2 "<num_1 <endl;} virtual void fun3 () {cout <"Base1: fun3" <num_1 <endl;} private: int num_1 ;}; class Base2 {public: Base2 (int num): num_2 (num) {} virtual void fun1 () {cout <"Base2: fun1" <num_2 <endl;} virtual void fun2 () {cout <"Base2 :: fun2 "<num_2 <endl;} virtual void fun3 () {cout <" Base2: fun3 "<num_2 <endl;} private: int num_2 ;}; class Base3 {public: Base3 (int num): num_3 (num) {} virtual void fun1 () {cout <"Base3: fun1" <num_3 <endl ;} virtual void fun2 () {cout <"Base3: fun2" <num_3 <endl;} virtual void fun3 () {cout <"Base3 :: fun3 "<num_3 <endl;} private: int num_3;}; class Derived1: public Base1 {public: Derived1 (int num): Base1 (num) {} virtual void fDer1_1 () {cout <"Derived1: fDer1_1 \ n" ;}// no overwrite virtual void fDer1_2 () {cout <"Derived1 :: fDer1_2 \ n ";}}; class Derived2: public Base1 {public: Derived2 (int num): Base1 (num) {} virtual void fun2 () {cout <" Derived2:: fun2 "<endl;} // only covers Base1: fun2 virtual void fDer2_1 () {cout <" Derived2: fDer2_1 \ n ";} virtual void fDer2_2 () {cout <"Derived2: fDer2_2 \ n" ;}}; class Derived3: public Base1, public Base2, public Base3 // multiple inheritance, no overwrite {public: Derived3 (int num_1, int num_2, int num_3): Base1 (num_1), Base2 (num_2), Base3 (num_3) {} virtual void fDer3_1 () {cout <"Derived3: fDer3_1 \ n";} virtual void fDer3_2 () {cout <"Derived3: fDer3_2 \ n" ;}}; class Derived4: public Base1, public Base2, public Base3 // multi-inheritance with overwrite {public: Derived4 (int num_1, int num_2, int num_3): Base1 (num_1), Base2 (num_2 ), base3 (num_3) {} virtual void fun1 () {cout <"Derived4: fun1 \ n" ;}// override the fun1 function of all base classes virtual void fDer4_1 () {cout <"Derived4: fDer4_1 \ n" ;}}; int main () {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 program running result is as follows: ----- Generally inherited from Base1, no cover ------ Base1: fun1 1 ----- Generally inherited from Base1, covering fun2 --- Derived2: fun2 ----- Multiple inheritance, no cover against Base1: fun1 1Base2: fun1 2Base3: fun1 3 ----- Multiple inheritance, covering fun1 between Derived4: Success :: fun1Derived4: fun1Press <RETURN> to close this window... */

General inheritance (no virtual function overwrite)

The Derived1 class inherits from the Base1 class and does not have any function that overrides the base class. Therefore, the two virtual functions of Dervied1 are added to the end of the virtual function table in sequence. The virtual function table of Derived1 is as follows:

General inheritance (overwrite with virtual functions)

Derived2 inherits from the Base1 class and overwrites fun2 () in the base class. Therefore, the Derived2: fun2 in the virtual function Table replaces Base: fun2. The new virtual function in the derived class is added to the end of the virtual function table. The virtual function table of Derived2 is as follows:

Multi-inheritance (no virtual function overwrite)

Derived3 is inherited from Base1, Base2, and Base3. Its virtual function table is as follows:

Base2 * pBase2 = new Derived3 (); pBase-> fun2 ();

Point the Base2 type pointer to the Derived3 instance, then the call will correspond to those functions in the Base2 virtual table.

Multiple inheritance (with virtual function overwrite)

The Derived4 class inherits from Base1, Base2, and Base3 and overwrites the fun1 functions of the three base classes. Its virtual function table is as follows:

Base1 * pBase1 = new Derived4 (); pBase1-> fun1 ();

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.