3_3 虚函数表

3_2_虚函数表.cpp

#include <iostream>

using namespace std;

class AC
{
public:
	AC() { cout << "AC no arg." << " ,this=" << hex << this << endl; }
	AC(const AC &ac) { cout << "AC copy." << " ,this=" << hex << this << endl; } // 拷贝函数中不用手动处理虚函数表,这是编译器帮助完成的
	virtual void f() { cout << "A f()." << " ,this=" << hex << this << endl; }
	virtual void g() { cout << "A g()." << " ,this=" << hex << this << endl; } // 由vptr将成员虚函数转为函数指针然后调用,调用时成员函数里的this并不是对象地址
	virtual void h() { cout << "A h()." << " ,this=" << hex << this << endl; }
	virtual ~AC(){}
};
class BC : public AC
{
public:
	BC() { cout << "BC no arg." << " ,this=" << hex << this << endl; }
	BC(const BC &bc) { cout << "BC copy." << " ,this=" << hex << this << endl; }
	virtual void g() { cout << "B g()." << " ,this=" << hex << this << endl; }
};

// 打印虚函数表中的虚函数地址
static void printVptr(AC *obj)
{
	long *ptmp = (long *)obj;
	long *vptr = (long *)(*ptmp);
	for (int i=0; i<=4; ++i)
	{
		cout << "vptr[" << i << "]=" << hex << vptr[i] << endl;
	}
	using PFUNC = void(*)(void);
	PFUNC fa = (PFUNC)vptr[0];
	PFUNC ga = (PFUNC)vptr[1];
	PFUNC ha = (PFUNC)vptr[2];
	fa();
	ga();
	ha();
}

int main_3_3_2()
{
	// 获取虚函数表中的保存的虚函数地址(vptr指针在类对象的首地址。 多重继承时,会有多个vptr指针)
	// 虚函数地址是跟类走的,与对象无关。 所以子类vptr与父类vptr可能指向相同虚函数地址,但是父类与子类中的虚函数表都是各有一份的
	// 父类
	cout << "------------------打印虚函数表中的虚函数地址,并调用(父类)------------------" << endl;
	AC *pa = new AC();
	printVptr(pa);
	// 子类
	cout << "------------------打印虚函数表中的虚函数地址,并调用(子类)------------------" << endl;
	BC *pb = new BC(); // 子类vptr指针与父类vptr指针是重合的,也就是 sizeof(子类) == sizeof(void*)
	printVptr(pb);
	// 指针多态
	cout << "------------------打印虚函数表中的虚函数地址,并调用(指针多态)------------------" << endl;
	AC *pc = new BC();
	printVptr(pc);
	// 伪多态
	cout << "------------------打印虚函数表中的虚函数地址,并调用(伪多态)------------------" << endl;
	BC bc;
	AC ac = bc; // 对象ac中vptr还是指向父类的虚函数表
	printVptr(&ac);
	// 引用多态  c++中实现多态即可以用指针还可以用引用
	cout << "------------------打印虚函数表中的虚函数地址,并调用(引用多态)------------------" << endl;
	AC &d = bc;
	printVptr(&d);
	/* 打印:
			------------------打印虚函数表中的虚函数地址,并调用(父类)------------------
			AC no arg. ,this=0x563eb59aeac0
			vptr[0]=563eb4529140
			vptr[1]=563eb4529070
			vptr[2]=563eb4528fa0
			vptr[3]=563eb4528f60
			vptr[4]=563eb4528f90
			A f(). ,this=0x7fca783af760
			A g(). ,this=0x7fca783af760
			A h(). ,this=0x7fca783af760
			------------------打印虚函数表中的虚函数地址,并调用(子类)------------------
			AC no arg. ,this=0x563eb59aeae0
			BC no arg. ,this=0x563eb59aeae0
			vptr[0]=563eb4529140
			vptr[1]=563eb4529210
			vptr[2]=563eb4528fa0
			vptr[3]=563eb4528f70
			vptr[4]=563eb4528f80
			A f(). ,this=0x7fca783af760
			B g(). ,this=0x7fca783af760
			A h(). ,this=0x7fca783af760
			------------------打印虚函数表中的虚函数地址,并调用(指针多态)------------------
			AC no arg. ,this=0x563eb59aeb00
			BC no arg. ,this=0x563eb59aeb00
			vptr[0]=563eb4529140
			vptr[1]=563eb4529210
			vptr[2]=563eb4528fa0
			vptr[3]=563eb4528f70
			vptr[4]=563eb4528f80
			A f(). ,this=0x7fca783af760
			B g(). ,this=0x7fca783af760
			A h(). ,this=0x7fca783af760
			------------------打印虚函数表中的虚函数地址,并调用(伪多态)------------------
			AC no arg. ,this=0x7fffb8658848
			BC no arg. ,this=0x7fffb8658848
			AC copy. ,this=0x7fffb8658850
			vptr[0]=563eb4529140
			vptr[1]=563eb4529070
			vptr[2]=563eb4528fa0
			vptr[3]=563eb4528f60
			vptr[4]=563eb4528f90
			A f(). ,this=0x7fca783af760
			A g(). ,this=0x7fca783af760
			A h(). ,this=0x7fca783af760
			------------------打印虚函数表中的虚函数地址,并调用(引用多态)------------------
			vptr[0]=563eb4529140
			vptr[1]=563eb4529210
			vptr[2]=563eb4528fa0
			vptr[3]=563eb4528f70
			vptr[4]=563eb4528f80
			A f(). ,this=0x7fca783af760
			B g(). ,this=0x7fca783af760
			A h(). ,this=0x7fca783af760
	 */

	delete pa;
	delete pb;
	delete pc;

	// linux下利用工具查看虚函数表:
		// g++ -fdump-class-hierarchy -fsyntax-only 3_2_虚函数表.cpp   // 会在当前目录下生成 3_2_虚函数表.cpp.002t.class 文件,可以vim查看

	// 静态链编:编译时就能确定函数调用
	// 动态链编:运行时,才能确定调用语句与被调用函数的绑定(常发生于多态时)

	return 0;
}

多重继承时内存结构
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值