C + + Virtual member function table vtable

Source: Internet
Author: User

This article describes how polymorphism is implemented, and how to implement polymorphism, even if it is not known to the program designer, but it is of great significance for deepening the understanding of polymorphism, so we will explain the mechanism of polymorphic implementation in this section.

In C + + through the virtual member function table vtable to achieve polymorphism, virtual function table is stored in the class virtual function of the entry address. In ordinary classes there is no virtual function table, only in a class with a virtual function (whether the virtual function is added or inherited from the virtual function) will have a virtual function table, usually the first address of the virtual member function table will be stored in the front of the object (in 32-bit operating system, the storage address is 4 bytes, So this first address will occupy the first four bytes of the object's space.

#include <iostream>
using namespace Std;

Class Base
{
Public
virtual void V1 () {}
virtual void V2 () {}
};

Class Derived:public Base
{
Public
virtual void V1 () {}
virtual void V2 () {}
};

int main ()
{
Base b;
Derived D;
Base *p;
p = &b;
P->v1 ();
P->v2 ();
p = &d;
P->v1 ();
P->v2 ();
return 0;
}

We define the two classes as shown in precedent 1, with two virtual functions V1 and V2 in each of the two classes, and we find their function entry addresses in the following table:

Virtual member function Function Entry Address Virtual member function Function Entry Address
Base::v1 00d15834 Derived::v1 00d15844
Base::v2 00d15838 Derived::v2 00d15848


The virtual function table stores the entry address of the virtual function. Let's look at the main function, first defining the base class object B in the main function, because there are virtual functions in class B, so there is a virtual function table, and the first address of the virtual function table is stored at the top of the storage space where the object is located, as the case may be. Of course, after declaring derived object D, the same is true in the object storage space that contains the virtual member function table address.


After defining a pointer p for a base class type, when we call the virtual function V1 or v2 through the base class pointer P, the system first looks for the virtual function table address in the first four bytes of the object that P points to, then finds the virtual function table in memory, then finds the entry address of the corresponding function in the table, and then accesses the function directly. When the P pointer points to a base class object, the virtual function table of the base class is accessed and the virtual function in the base class is called. When the P pointer points to a derived class object, it accesses the virtual function table of the derived class, which is stored in the virtual function table of the derived class as the entry address of the virtual function in the derivation class, so the virtual function in the derived class is called.

Using polymorphism can reduce the efficiency of the program, use polymorphic programs to use more storage space, store virtual function tables and other content, and in the call function to go to the virtual function table to query function entry address, which increases the program run time. in the design of the program, the programmer can choose the use of polymorphism, for the required functions to use polymorphism, for other functions do not use polymorphism. Typically, if a class needs to be a base class and expects to modify the functionality of a member function in a derived class and accesses the function as a pointer or reference when using the class object, the function is declared as a virtual function.

C + + Virtual member function table vtable

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.