C ++ object layout and polymorphism implementation class with virtual functions

Source: Internet
Author: User

What happens if there is a virtual function in the class? We know that when a class has a virtual function, the compiler will generate a virtual function table for the class and insert a pointer to the virtual function table in each of its objects, usually this pointer is inserted at the starting position of the object. The so-called virtual function table is actually a pointer array, in which the Pointer Points to the real function start address. Let's verify that the class C040, which defines a non-member variable, contains a virtual function.

struct C040
{
 virtual void foo() {}
};

Run the following code to print its size and content in the object.

PRINT_SIZE_DETAIL(C040)
Result:

The size of C040 is 4
The detail of C040 is 40 b4 45 00

It is 4 bytes in size, that is, it contains a pointer, And the Pointer Points to the address 0x0045b440.

Define an empty class C050, which is derived from the class C040.

struct C050 : C040
{};

Because Virtual functions are inherited and maintained as virtual functions. The C050 object should also contain a pointer to the C050 virtual function table.

Run the following code to print its size and content in the object.

PRINT_SIZE_DETAIL(C050)
Result:

The size of C050 is 4
The detail of C050 is 44 b4 45 00

It is also 4 bytes in size, that is, it contains a pointer to the virtual function table (hereinafter referred to as the virtual table pointer ).

Virtual tables are class-level, and all objects of the class share the same virtual table. We can generate two objects in the class C040, and then observe the object address, virtual table pointer address, virtual table address, and the value of the entries in the virtual table (that is, the function address to which the object points) for verification.

Run the following code:

C040 obj1, obj2;
PRINT_VTABLE_ITEM(obj1, 0, 0)
PRINT_VTABLE_ITEM(obj2, 0, 0)

The result is as follows:

obj1 : objadr:0012FDC4 vpadr:0012FDC4 vtadr:0045B440 vtival(0):0041D834
obj2 : objadr:0012FDB8 vpadr:0012FDB8 vtadr:0045B440 vtival(0):0041D834

(Note: The first column is the object name, the second column (objadr) is the object's memory address, the third column (vpadr) is the virtual table pointer address, and the fourth column (vtadr) it is the address of the virtual table, the Fifth Column (vtival (n) is the value of the entries in the virtual table, and n is the index of the entries, starting from 0. Later)

Sure enough, the object address is different. The virtual table pointer (vpadr) is located at the starting position of the object, so its address is the same as that of the object. The virtual table pointer of the two objects points to the same virtual table, so the value of (vtadr) is the same, and the value of the first object (vtival (0) in the virtual table is of course the same.

Related Article

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.