Class of C + + object layout and multi-state implementation with virtual functions

Source: Internet
Author: User

What happens if a virtual function exists in a class? We know that when there is a virtual function in a class, the compiler generates a virtual function table for the class and inserts a pointer to the virtual function table in each of its objects, usually the pointer is inserted at the beginning of the object. The so-called virtual function table is actually an array of pointers, where the pointer points to the REAL function start address. Let's verify that we define a class C040 with no member variables, which contains a virtual function.

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

Run the following code to print its size and the contents of the object.

PRINT_SIZE_DETAIL(C040)

The results are:

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

Sure enough, it's 4 bytes in size, that contains a pointer to the address of 0x0045b440.

Also defines an empty class C050, derived from class C040.

struct C050 : C040
{};

Because the virtual function is inherited and is maintained as a virtual function. Then the class-C050 object should also contain a pointer to the C050 's virtual function table.

Run the following code to print its size and the contents of the object.

PRINT_SIZE_DETAIL(C050)

The results are:

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

Sure enough, its size is also 4 bytes, which contains a pointer to the virtual function table (after the virtual table) (after the virtual table pointer).

Virtual tables are class-level, and all objects of a class share the same virtual table. We can generate two objects of the class C040, and then verify them by looking at the address of the object, the address of the virtual table pointer, the virtual table address, and the value of the entry in the virtual table (that is, the address of the function being pointed to).

Run the following code:

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

The results are 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, and the second (OBJADR) is the memory address of the object. The third column (VPADR) is the virtual table pointer address, the fourth column (VTADR) is the address of the virtual table, the fifth column (Vtival (n)) is the value of the entry in the virtual table, and N is the index of the entry, starting from 0. After the same)

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.