多态-虚函数表

VS的对象内存分析:

/d reportSingleClassLayout+类名

 使用方法:项目 -- 属性 -- C/C++ -- 命令行--其他选型(D) 添加命令.如图所示:

多态-虚函数表_第1张图片

Father类:

class Father {
public:
	virtual void Func1() { cout << "Father::Func1" << endl; }
	virtual void Func2() { cout << "Father::Func2" << endl; }
	virtual void Func3() { cout << "Father::Func3" << endl; }
	void Func4() { cout << "Father::Func4" << endl; }
public:
	int x = 200;
	int y = 300;
	static int z;
};
int Father::z = 0;

Father的内存分析: 

/d reportSingleClassLayoutFather

多态-虚函数表_第2张图片

 

Son类: public Father

class Son:public Father
{
public:
	void Func1() { cout << "Son::Func1" << endl; }
	virtual void Func5() { cout << "Son::Func5" << endl; }

private:
	int m = 400;
	int n = 500;
};

Son类的内存分析: 

/d reportSingleClassLayoutSon

 多态-虚函数表_第3张图片

 子类的虚函数表构建过程:

(1)直接复制父类的虚函数表

多态-虚函数表_第4张图片

(2)如果子类重写了父类的某个虚函数,那么就在这个虚函数表中进行相应的替换

 多态-虚函数表_第5张图片

(3)如果子类中添加了新的虚函数,就把这个虚函数添加到虚函数的表中(在尾部添加)

多态-虚函数表_第6张图片

 

 注意:所有子类共用同一张虚函数表

main函数:

int main(void) {
	Son son;
	Father* father = &son;
	cout << "----Father* father = &son----" << endl;
	son.Func1();
	son.Func2();
	son.Func3();
	son.Func4();
	son.Func5();
	Father* son2 = new Son();
	cout << "----Father* son2 = new Son()----" << endl;
	son2->Func1();
	son2->Func2();
	son2->Func3();
	son2->Func4();
	//son2->Func5();//错误使用
	return 0;
}

结果:

多态-虚函数表_第7张图片

 全部代码(测试):

#include
using namespace std;
//vs的对象内存分析:/d1 reportSingleClassLayout+类名
class Father {
public:
	virtual void Func1() { cout << "Father::Func1" << endl; }
	virtual void Func2() { cout << "Father::Func2" << endl; }
	virtual void Func3() { cout << "Father::Func3" << endl; }
	void Func4() { cout << "Father::Func4" << endl; }
public:
	int x = 200;
	int y = 300;
	static int z;
};
int Father::z = 0;

class Son:public Father
{
public:
	void Func1() { cout << "Son::Func1" << endl; }
	virtual void Func5() { cout << "Son::Func5" << endl; }

private:
	int m = 400;
	int n = 500;
};



typedef void(*func)(void);

void test(void) {
	Father father;
	cout << "sizeof(father): " << sizeof(father) << endl;
	cout << "(int*)&father: " << (int*)&father << endl;
	cout << "&father: " << &father << endl;
	int* vptr = (int*)*(int*)(&father);
	cout << "调用虚函数Func1:" << endl;
	((func) * (vptr + 0))();
	cout << "调用虚函数Func2:" << endl;
	((func) * (vptr + 1))();
	cout << "调用虚函数Func3:" << endl;
	((func) * (vptr + 2))();

	cout << "father.x地址: " << endl;
	cout << &father.x << endl;
	cout << (int*)((int)&father + 4) << endl;
	cout << "father.x:" << endl;
	cout << father.x << endl;
	cout << *(int*)((int)&father + 4) << endl;

	cout << "father.y地址: " << endl;
	cout << &father.y << endl;
	cout << (int*)((int)&father + 8) << endl;
	cout << "father.y:" << endl;
	cout << father.y << endl;
	cout << *(int*)((int)&father + 8) << endl;
}

int main(void) {
	test();
	Son son;
	Father* father = &son;
	cout << "----Father* father = &son----" << endl;
	son.Func1();
	son.Func2();
	son.Func3();
	son.Func4();
	son.Func5();
	Father* son2 = new Son();
	cout << "----Father* son2 = new Son()----" << endl;
	son2->Func1();
	son2->Func2();
	son2->Func3();
	son2->Func4();
	//son2->Func5();//错误使用
	return 0;
}

你可能感兴趣的:(c++)