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.