Virtual inheritance of the exploration of C + + object layout and polymorphism implementation

Source: Internet
Author: User
Tags inheritance

Here we look at virtual inheritance. First look at this C020 class, which inherits from C010 virtual inheritance:}

struct C010
{
 C010() : c_(0x01) {}
 void foo() { c_ = 0x02; }
 char c_;
};
struct C020 : public virtual C010
{
 C020() : c_(0x02) {}
 char c_;
};

Run the following code to view the memory layout of the object:

PRINT_SIZE_DETAIL(C020)

The results are:

The size of C020 is 6
The detail of C020 is c0 c2 45 00 02 01

It is obvious that the beginning of the object is a pointer, then a member variable of the subclass, followed by a member variable of the parent class. Unlike previous discussions, because virtual inheritance was used, the member variables of the parent class were put to the last side.

Run the following code:

C020 c020;
c020.C010::c_ = 0x04;

Because the variables in the subclass and the variables in the parent class have the same name, we must use this method to access the member variables that belong to the parent class, which is not required in normal circumstances. Let's take a look at the following assembly code for this line of code:

0042387E mov eax,dword ptr [ebp+FFFFF82Ch]
00423884 mov ecx,dword ptr [eax+4]
00423887 mov byte ptr [ebp+ecx+FFFFF82Ch],4

Previously said that the beginning of the object is a pointer, the 1th line of instructions to take the value of this pointer, the 2nd row of this pointer to the address after 4 bytes after the value (as a 4-byte value) taken out. After the execution of this sentence we look at the ECX register, we know that the value taken out is 5. The last line is the true assignment instruction, which gets the destination address of the assignment by adding the value in the ECX at the beginning of the object (that is, [ebp+fffff32ch]) to the offset value (that is, 5). Joining the front object layout output, we can find the 5-byte offset from the start address of the object, just to get the address of the member variable of the parent class. This allows us to roughly analyze the object layout of a subclass that is directly virtual inheritance.

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.