VS2008 C + + Object Memory layout (2): Simple inheritance

Source: Internet
Author: User

This time we add a subclass with no virtual functions for both the parent class and the subclass:

class CParent
{
public:
 int parent_a;
 int parent_b;

public:
 void parent_f1()
 {
  parent_a = 0x10;
 }
 void parent_f2()
 {
  parent_b = 0x20;
 }
};

class CChild : public CParent
{
public:
 int child_a;
 int child_b;

public:
 void child_f1()
 {
  child_a = 0x30;
 }
 void child_f2()
 {
  child_b = 0x40;
 }
};

1.1.1 Memory Layout

Assign a few values to these member variables first:

child.parent_a = 1;
0041138E C7 05 50 71 41 00 01 00 00 00 mov         dword ptr [child (417150h)],1
     child.parent_b = 2;
00411398 C7 05 54 71 41 00 02 00 00 00 mov         dword ptr [child+4 (417154h)],2
     child.child_a = 3;
004113A2 C7 05 58 71 41 00 03 00 00 00 mov         dword ptr [child+8 (417158h)],3
     child.child_b = 4;
004113AC C7 05 5C 71 41 00 04 00 00 00 mov         dword ptr [child+0Ch (41715Ch)],4

Observe the contents of the memory area that &child refers to:

01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 .......

Obviously, vs merges the data members of the parent class and subclass into the same memory area, but also does not add anything extra to it, the &child pointer points to the first member Parent_a.

1.1.2 Function call

Observe function calls:

child.parent_f1();
00411840 B9 50 71 41 00   mov         ecx,offset child (417150h)
00411845 E8 78 F9 FF FF   call        CParent::parent_f1 (4111C2h)
     child.child_f1();
0041184A B9 50 71 41 00   mov         ecx,offset child (417150h)
0041184F E8 64 F9 FF FF   call        CChild::child_f1 (4111B8h)

is no different from a function call to a simple parent class. Pass the this pointer with ECX.

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.