C++虚函数表调用学习

知识点是看 陈皓大哥的博客,代码也参考了他的,不过做了很小的改动。

原文链接:http://blog.csdn.net/haoel/article/details/1948051

 

#include "stdio.h"

class Base
{
public:
	virtual void Fun1()
	{
		printf("Call Base Fun1\n");
	}
	virtual void Fun2()
	{
		printf("Call Base Fun2\n");
	}
	virtual void Fun3()
	{
		printf("Call Base Fun3\n");
	}
private:
	virtual void Fun4()
	{
		printf("Call Base Fun4\n");
	}
};

class Derived : public Base
{
public:
	virtual void Fun1()
	{
		printf("Call Derived Fun1\n");
	}
	virtual void Fun5()
	{
		printf("Call Fun5\n");
	}
};

typedef void(*Fun)(void);

void main(void)
{
	Derived lDerived;

	int** lpVtableAdd = (int**)&lDerived;


	for (int i = 0; i< 5; ++i)
	{
		Fun lpFun = (Fun)(lpVtableAdd[0][i]);

		lpFun();
	}

	printf("First add of Base:%d, and first virtual Fun add:%d\n",(int*)(&lDerived),&lpVtableAdd);

}


看懂了这些代码 应该对虚函数表就清楚啦吧,O(∩_∩)O~

 

 

posted @ 2013-07-31 20:26  坚固66  阅读(139)  评论(0编辑  收藏  举报