C + + virtual functions and virtual function tables

Source: Internet
Author: User

Leading

The above blog post describes the structure of virtual function tables in base classes and derived classes when virtual functions exist in a base class.

What is the structure of a function table when a derived class also defines a virtual function?

Let's look at the following sample code:

1#include <iostream>2 3 using namespacestd;4 5 classA6 {7  Public:8     Virtual voidFunca () {cout<<"A"<<Endl;}9 };Ten  One classB A { -  Public: -     Virtual voidFUNCB () {cout<<"B"<<Endl;} the }; -  - classE | PublicA PublicB - { +  Public: -     Virtual voidFUNCC () {cout<<"C"<<Endl;} + }; A  at intMain () - { - c C; -Cin.Get(); -}

Both Class A and Class B have a virtual function, and class C inherits A and B. In VS2010, look at the variables:

, only the virtual function table addresses that inherit from a and B are shown in the local variables. So what about C's own virtual function?

First, look at the address of A virtual function table:

As you can see, the first 4 bytes in the virtual function table are the addresses of the virtual functions in A (red box). At the same time, followed by 4 bytes with content, and then the virtual function table is the end of the 4 0.

It can be guessed that this should be the virtual function address of C. Let's take a look at the virtual function table for B:

As you can see, only the virtual function of B in the virtual function table is an address. To confirm the above guess, the function pointer is incremented back from A::funca one time, which should be the call to C::func:

int Main () {    void(*pfun) ();    c C;     = &C;     = (pfun) * * ((int*) p)    ; // Call A::funca = (pfun) * (* (int1);          // Call C::FUNCC     cin. Get ();}

Output: Confirms our guess.

What if you add a class D to inherit C?

class  Public c{    virtualvoid  Funcd () {}}; int Main () {    d D;    Cin. Get ();}

Variable:

As shown in C, there are only two virtual function tables.

Memory:

As you can see, there are two addresses after A::funca, which can be inferred as C::FUNCC and D::funcd's address.

Summarize:

1. In multi-inheritance, the number of virtual function tables of a derived class is determined by the number of "top-level" base classes it inherits: How many of these base classes there are virtual function tables.

2. A derived class's own virtual function is appended to the first virtual function table.

For example, the following inheritance:

Assuming that each class X has a funcx virtual function, the virtual and virtual function tables in G are as follows:

C + + virtual and virtual function tables

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.