Explore C + + virtual function table

Source: Internet
Author: User

Implementation of polymorphism in C + +

As we all know, polymorphism in C + + is implemented on the basis of virtual functions, calling the derived class (or base class) its own member function with a base-class pointer to the derived class. So, how is it implemented?

In fact, it is implemented by the virtual function table, virtual function table is to save the virtual function of the address of a table, if a class has a virtual function, when the program runs, the compiler by looking in the virtual function table of the corresponding virtual function address to call the function.

There are several types of inheritance for objects:

1. Single Inheritance

2. Multiple inheritance

3. Repeat inheritance (Diamond inheritance)

4. Virtual inheritance

Let's take a look at the various inherited memory layouts:

Single inheritance

650) this.width=650; "title=" Picture 1.png "alt=" Wkiol1bv6nsjyaljaaaoqsh86c0798.png "src=" http://s3.51cto.com/wyfs02/M00 /7d/c7/wkiol1bv6nsjyaljaaaoqsh86c0798.png "/>

Structure of a single inheritance

Run the following program:

#include <iostream>using namespace std;typedef void (*pfun) ();class b{public:      b ()  :_b (0) {}     virtual void fun1 ()       {          cout <<  " B::FUN1 () " << endl;     }     virtual  Void fun2 ()      {           cout <<  "b::fun2 ()"  << endl;     }     private:     int _b;}; Class d:public b{public:     d ()  :_d (1) {}     &NBSP;VOID&NBSP;FUN1 ()      {           cout <<  "d::fun1 ()"  << endl;  &nbsP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VIRTUAL&NBSP;VOID&NBSP;FUN3 ()      {           cout <<  "D::fun3 ()"  <<  endl;     }private:     int _d;}; VOID&NBSP;PRINTVT (INT&NBSP;B)//Call virtual function {     pfun pfun = null based on function address in virtual function table ;     int *ptr =  (int *) b;     int  i = 0;     while  (Ptr[i])      {           pfun =  (pfun) ptr[i];//converts the address of a virtual function to a function pointer            pfun ();//Call function with function pointer            i++;     }}int main () {     d d; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRINTVT (* (int *) &d);//Remove the address of the virtual function table      getchar ();      return  0;}

We can see in the memory window:

650) this.width=650; "style=" width:716px;height:225px; "title=" Picture 2.png "alt=" Wkiol1bv7tmryy2yaaalmvvo1sq116.png " Src= "Http://s5.51cto.com/wyfs02/M00/7D/C7/wKioL1bv7tmRYY2YAAAlmvvo1sQ116.png" width= "773" height= "221"/>

The function of the above program is to call the function in the virtual function table corresponding to the address of the function, the program run the result is as follows:

650) this.width=650; "title=" Picture 3.png "alt=" Wkiol1bv77-y-7qeaaag9jenhly093.png "src=" http://s5.51cto.com/wyfs02/M01 /7d/c7/wkiol1bv77-y-7qeaaag9jenhly093.png "/>

Now we can know the memory layout of a single inheritance

650) this.width=650; "title=" Picture 4.png "alt=" Wkiom1bv8bkrjhvkaaaixokab6s174.png "src=" http://s1.51cto.com/wyfs02/M00 /7d/cb/wkiom1bv8bkrjhvkaaaixokab6s174.png "/>

Single-Inheritance object model

The single-Inheritance memory layout is:

1) The address of the virtual function table is placed at the front of the object, and then the address of the member variable is placed in the order in which it is inherited.

2) The virtual table first places the address of the virtual function that the derived class inherits from the base class in the order of inheritance, and then the address of the virtual function of the derived class itself.

3) The address of the base class virtual function is overridden by the virtual function address of the derived class that satisfies the coverage condition.

Covered:

1) different scopes (base classes and derived classes)

2) same function name, same parameter list, same return value type (except covariance)

3) for virtual function (virtual)

Note: Covariance refers to a pointer to a base class type that returns a base class virtual function, and the return value of a derived class virtual function is a pointer to a derived class type.

Multiple inheritance

Not to be continued ...

Explore 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.