这次我们添加一个子类,父类和子类均不带虚函数:
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 内存布局
先给这些成员变量赋几个值:
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
观察&child所指的内存区内容:
0x00417150 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 ................
很明显,VS将父类和子类的数据成员合并到同一块内存区域,但是同样没有往里面加额外的东西,&child指针将指向第一个成员parent_a。
1.1.2 函数调用
观察函数调用:
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)
和简单父类的函数调用没什么两样。用ECX传递this指针。