原理:http://blog.csdn.net/haoel/article/details/1948051/
感谢作者分享
#include <iostream>
class Rect
{
public:
virtual void drawRect()
{
std::cout << "Rect::drawRect()" << std::endl;
}
};
class Circle
{
public:
virtual void drawCircle()
{
std::cout << "Circle::drawCircle()" << std::endl;
}
};
class Painter : public Rect, public Circle
{
public:
};
int main()
{
typedef void(*Function)();
Painter painter;
//std::cout << sizeof(Painter) << std::endl;
for (long i = 0; i < 2; ++i)
{
//vTable List
long* vTable = ((long*)(&painter)) + i;
//vTable
long* vFunctionList = (long*)(*vTable);
for (int j = 0;; ++j)
{
//vFunction
Function function = (Function)vFunctionList[j];
if (!function)
{
break;
}
function();
}
}
std::cin.get();
}
//1.抽象类被派生并实例化以后,实例的首地址为一个指针,该指针指向一个一维数组,数组中的每个元素都是一个函数指针。
//2.如果派生类派生自多个抽象类,则子类实例中含有多个指向数组的指针。
//3.如果子类中也有虚函数,则子类中的虚函数指针被追加到第一个基类虚函数表中。