• 博客访问： 47373
• 博文数量： 11
• 博客积分： 140
• 博客等级： 入伍新兵
• 技术积分： 208
• 用 户 组： 普通用户
• 注册时间： 2012-06-22 00:51

2013年（10）

2012年（1）

2013-09-29 22:39:39

## 1.     简单虚表实例

C++一个很大的特性就是多态，动态多态就是采用虚函数实现。

1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. public:
6.     virtual void vFun1(){cout<<"vFun1() is invoked"<<endl;}
7.     virtual void vFun2(){cout<<"VFun2() is invoked"<<endl;}
8.     virtual void vFun3(){cout<<"vFun3() is invoked"<<endl;}
9. };

10. typedef void (*fun)();
11. int main()
12. {
13.     A a;
14.     cout<<hex<<"(int) &a=0x"<<(unsigned int)(&a)<<endl;
15.     cout<<hex<<"vtbl=0x"<<(unsigned int)*((unsigned int*)(&a))<<endl;
16.     cout<<hex<<"vtbl::1stItem=0x"<<(unsigned int)(*( (unsigned int*)(*(unsigned int*)(&a)) ))<<endl;
17.     cout<<hex<<"vtbl::2ndItem=0x"<<(unsigned int)(*( (unsigned int*)(*(unsigned int*)(&a))+1 ))<<endl;
18.     cout<<hex<<"vtbl::3rdItem=0x"<<(unsigned int)(*( (unsigned int*)(*(unsigned int*)(&a))+2 ))<<endl;
19.     fun Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&a))));
20.     Fun();
21.     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&a))+1));
22.     Fun();
23.     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&a))+2));
24.     Fun();

25.     return 0;
26. }

## 2.     函数全体覆盖(override)后的情况：

1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. public:
6.     virtual void vFun1(){cout<<"vFun1() is invoked"<<endl;}
7.     virtual void vFun2(){cout<<"VFun2() is invoked"<<endl;}
8.     virtual void vFun3(){cout<<"vFun3() is invoked"<<endl;}
9. };
10. class B:public A
11. {
12. public:
13.     void vFun1(){cout<<"B::vFun1() is invoked"<<endl;}
14.     void vFun2(){cout<<"B::VFun2() is invoked"<<endl;}
15.     void vFun3(){cout<<"B::vFun3() is invoked"<<endl;}
16. };

17. typedef void (*fun)();
18. int main()
19. {
20.     B b;
21.     cout<<hex<<"(int) &a=0x"<<(unsigned int)(&b)<<endl;
22.     cout<<hex<<"vtbl=0x"<<(unsigned int)*((unsigned int*)(&b))<<endl;
23.     cout<<hex<<"vtbl::1stItem=0x"<<(unsigned int)(*( (unsigned int*)(*(unsigned int*)(&b)) ))<<endl;
24.     cout<<hex<<"vtbl::2ndItem=0x"<<(unsigned int)(*( (unsigned int*)(*(unsigned int*)(&b))+1 ))<<endl;
25.     cout<<hex<<"vtbl::3rdItem=0x"<<(unsigned int)(*( (unsigned int*)(*(unsigned int*)(&b))+2 ))<<endl;
26.     fun Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))));
27.     Fun();
28.     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))+1));
29.     Fun();
30.     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))+2));
31.     Fun();

32.     return 0;
33. }

0x004112b2, 0x4112a30x4112a8参见前面QuickWatch图中定义。

# 多重继承情况 多重继承会有多个虚函数表，每个基类一个虚表。 子类函数指针将覆盖基类所有虚表中的同名同参函数指针。 子类中新增的虚函数将只增加到第一个虚函数表的后面。但不能用基类指针直接访问子类新增的虚函数，编译会报错，即使用函数指针访问也会出现crash。 点击(此处)折叠或打开 #include <iostream> using namespace std; class A { public:     virtual void vFun1(){cout<<"A::vFun1() is invoked"<<endl;}     virtual void vFun2(){cout<<"A::vFun2() is invoked"<<endl;}     virtual void vFun3(){cout<<"A::vFun3() is invoked"<<endl;} }; class C { public:     virtual void vFun1(){cout<<"C::vFun1() is invoked"<<endl;}     virtual void vFun2(){cout<<"C::Fun2() is invoked"<<endl;} }; class D { public:     virtual void vFun2(){cout<<"D::VFun2() is invoked"<<endl;}     virtual void vFun3(){cout<<"D::vFun3() is invoked"<<endl;} }; class B:public A,C,D { public:     void vFun3(){cout<<"B::vFun3() is invoked"<<endl;}     virtual void vFun4(){cout<<"B::vFun4() is invoked"<<endl;} }; typedef void (*fun)(); int main() {     B b;     fun Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))));     Fun();     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))+1));     Fun();     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))+2));     Fun();     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))+3));     Fun();     return 0; } 5.     通过虚表中的函数指针调用基类中的私有虚函数 虽然子类实例不能直接调用基类的私有虚函数，但可以通过虚表地址直接访问基类的私有虚函数， 点击(此处)折叠或打开 #include <iostream> using namespace std; class A { Private:     virtual void vFun1(){cout<<"A::vFun1() is invoked"<<endl;}     virtual void vFun2(){cout<<"A::vFun2() is invoked"<<endl;} public:     virtual void vFun3(){cout<<"A::vFun3() is invoked"<<endl;} }; class B:public A { public: }; typedef void (*fun)(); int main() {     B b;          fun Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b)))); //OK, call private A::vFun1()     Fun();     Fun=(fun)(*((unsigned int*)(*(unsigned int*)(&b))+1)); //OK, call private A::vFun2()     Fun();     b.vFun1(); //error C2248: 'A::vFun1' : cannot access private member declared in class 'A'     b.vFun3(); //OK, call public A::vFun2()     return 0; }

0

wucaiii2013-09-30 15:24:45

<<hex<<"(int) &a=0x"<<(unsigned int)(&b)<<endl;

cout<<hex<<"vtbl=0x"<<(unsigned int)*((unsigned int*)(&b))<<endl;
pptv破解版2013无限制   http://www.pptvpj.com

cout<<hex<<"vtbl::1stItem=0x"<<(unsigned int)(*( (unsigned int*)(*(unsigned int*)(&b)) ))<<endl;

cout<<hex<<"vtbl::2ndItem=0x"<<(un