虚表剖析

虚基表是通过一块连续的内存来存储虚函数的地址。这张表解决了继承、虚函数(重写)的问题。

在有虚函数的对象实例中都存在一张虚表,虚函数表就像是一张地图,指明了实际应该调用的虚函数


一、探索单继承对象模型

1、没有覆盖

#include<iostream>
using namespace std;

class Base
{
public:
	virtual void fun1()
	{cout << "Base::fun1" << endl;}
	virtual void fun2()
	{cout << "Base::fun2" << endl;}
private:
	int _a = 1;
};
class Derive : public Base
{
public:
	virtual void fun3()
	{cout << "Derive::fun3" << endl;}
	virtual void fun4()
	{cout << "Derive::fun4" << endl;}
private:
	int _b = 2;
};
typedef void(*FUNC)();
void PrintVTable(int * VTable)
{
	cout << "虚表地址:" << VTable << endl;
	for (int i = 0; VTable[i] != 0; i++)
	{
		printf("第%d个虚函数地址:0x%x,->", i, VTable[i]);
		FUNC f = (FUNC)VTable[i];
		f();
	}
	cout << endl;
}
void test()
{
	Base b1;
	Derive d1;

	int* Vtable1 = (int*)(*(int*)&b1);
	int* Vtable2 = (int*)(*(int*)&d1);

	PrintVTable(Vtable1);
	PrintVTable(Vtable2);
}
int main()
{
	test();
	return 0;
}


说明:

(1)虚函数按照其声明顺序存在于虚表中

(2)在派生类中,前面基类的虚函数,后面是派生类的虚函数

2、有覆盖

#include<iostream>
using namespace std;

class Base
{
public:
	virtual void fun1()
	{cout << "Base::fun1" << endl;}
	virtual void fun2()
	{cout << "Base::fun2" << endl;}
private:
	int _a = 1;
};
class Derive : public Base
{
public:
	virtual void fun1()
	{cout << "Derive::fun1" << endl;}
	virtual void fun3()
	{cout << "Derive::fun3" << endl;}
	virtual void fun4()
	{cout << "Derive::fun4" << endl;}
private:
	int _b = 2;
};
typedef void(*FUNC)();
void PrintVTable(int * VTable)
{
	cout << "虚表地址:" << VTable << endl;
	for (int i = 0; VTable[i] != 0; i++)
	{
		printf("第%d个虚函数地址:0x%x,->", i, VTable[i]);
		FUNC f = (FUNC)VTable[i];
		f();
	}
	cout << endl;
}
void test()
{
	Base b1;
	Derive d1;

	int* Vtable1 = (int*)(*(int*)&b1);
	int* Vtable2 = (int*)(*(int*)&d1);

	PrintVTable(Vtable1);
	PrintVTable(Vtable2);
}
int main()
{
	test();
	return 0;
}


通过基类的引用或指针调用虚函数时,调用基类还是派生类的虚函数,要根据运行时引用(指针)实际引用(指针)的类型确定调用非虚函数时,则无论基类指向的是何种类型,都调用的是基类的函数。

派生类的虚函数表生成:

(1)先拷贝基类的虚函数表

(2)如果派生类重写了基类的某个虚函数,就同位置的基类虚函数

(3)跟上派生类自己定义的虚函数

Base b1;
Derive d1;

pBase = &b1;  指向基类,获取到的是基类的虚表指针
PBase->Fun();

pBase = &d1;
PBase->fun();  指向派生类,获取到的是派生类的虚表指针

二、探索多继承的内存分布

1、有覆盖的

#include<iostream>
using namespace std;

class Base1
{
public:
	virtual void fun1()
	{
		cout << "Base1::fun1()" << endl;
	}
	virtual void fun2()
	{
		cout << "Base1::fun2()" << endl;
	}
private:
	int b1 = 1;
};
class Base2
{
public:
	virtual void fun1()
	{
		cout << "Base2::fun1()" << endl;
	}
	virtual void fun2()
	{
		cout << "Base2::fun2()" << endl;
	}
private:
	int b2 = 2;
};

class Derive :public Base1, public Base2
{
public:
	virtual void fun1()
	{
		cout << "Derive::fun1()" << endl;
	}
	virtual void fun2()
	{
		cout << "Derive::fun2()" << endl;
	}
	virtual void fun3()
	{
		cout << "Derive::fun3()" << endl;
	}
private:
	int d = 5;
};

typedef void(*FUNC)();
void PrintVTable(int * VTable)
{
	cout << "虚表地址:" << VTable << endl;
	for (int i = 0; VTable[i] != 0; i++)
	{
		printf("第%d个虚函数地址:0x%x,->", i, VTable[i]);
		FUNC f = (FUNC)VTable[i];
		f();
	}
	cout << endl;
}
void test()
{
	Base1 b1;
	Base2 b2;
	Derive d;

	int* Vtable1 = (int*)(*(int*)&b1);
	int* Vtable2 = (int*)(*(int*)&b2);
	int* Vtable3 = (int*)(*(int*)&d);

	PrintVTable(Vtable1);
	PrintVTable(Vtable2);
	PrintVTable(Vtable3);
}
int main()
{
	test();
	return 0;
}



2、没有覆盖的

#include<iostream>
using namespace std;

class Base1
{
public:
	virtual void fun1()
	{
		cout << "Base1::fun1()" << endl;
	}
	virtual void fun2()
	{
		cout << "Base1::fun2()" << endl;
	}
private:
	int b1 = 1;
};
class Base2
{
public:
	virtual void fun1()
	{
		cout << "Base2::fun1()" << endl;
	}
	virtual void fun2()
	{
		cout << "Base2::fun2()" << endl;
	}
private:
	int b2 = 2;
};

class Derive :public Base1, public Base2
{
public:
	virtual void fun3()
	{
		cout << "Derive::fun3()" << endl;
	}
private:
	int d = 5;
};

typedef void(*FUNC)();
void PrintVTable(int * VTable)
{
	cout << "虚表地址:" << VTable << endl;
	for (int i = 0; VTable[i] != 0; i++)
	{
		printf("第%d个虚函数地址:0x%x,->", i, VTable[i]);
		FUNC f = (FUNC)VTable[i];
		f();
	}
	cout << endl;
}
void test()
{
	Base1 b1;
	Base2 b2;
	Derive d;

	int* Vtable1 = (int*)(*(int*)&b1);
	int* Vtable2 = (int*)(*(int*)&b2);
	int* Vtable3 = (int*)(*(int*)&d);

	PrintVTable(Vtable1);
	PrintVTable(Vtable2);
	PrintVTable(Vtable3);
}
int main()
{
	test();
	return 0;
}


三、菱形继承

#include<iostream>
using namespace std;
class A
{
public:
	virtual void f()
	{
		cout << "A::f()" << endl;
	}
private:
	int _a = 1;
};
class B: public A
{
public:
	virtual void f()
	{
		cout << "B::f()" << endl;
	}
private:
	int _b = 2;
};
class C : public A
{
public:
	virtual void f()
	{
		cout << "C::f()" << endl;
	}
private:
	int _c = 3;
};
class D : public B,public C
{
public:
	virtual void f()
	{
		cout << "D::f()" << endl;
	}
private:
	int _d = 4;
};
int main()
{
	D d;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值