知识点是看 陈皓大哥的博客,代码也参考了他的,不过做了很小的改动。
原文链接:http://blog.csdn.net/haoel/article/details/1948051
#include "stdio.h" class Base { public: virtual void Fun1() { printf("Call Base Fun1 "); } virtual void Fun2() { printf("Call Base Fun2 "); } virtual void Fun3() { printf("Call Base Fun3 "); } private: virtual void Fun4() { printf("Call Base Fun4 "); } }; class Derived : public Base { public: virtual void Fun1() { printf("Call Derived Fun1 "); } virtual void Fun5() { printf("Call Fun5 "); } }; 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 ",(int*)(&lDerived),&lpVtableAdd); }
看懂了这些代码 应该对虚函数表就清楚啦吧,O(∩_∩)O~