日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++虚函数与虚函数表

發(fā)布時(shí)間:2024/4/13 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++虚函数与虚函数表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

多態(tài)性可分為兩類:靜態(tài)多態(tài)和動(dòng)態(tài)多態(tài)。函數(shù)重載和運(yùn)算符重載實(shí)現(xiàn)的多態(tài)屬于靜態(tài)多態(tài),動(dòng)態(tài)多態(tài)性是通過(guò)虛函數(shù)實(shí)現(xiàn)的。

每個(gè)含有虛函數(shù)的類有一張?zhí)摵瘮?shù)表(vtbl),表中每一項(xiàng)是一個(gè)虛函數(shù)的地址, 也就是說(shuō),虛函數(shù)表的每一項(xiàng)是一個(gè)虛函數(shù)的指針。

沒(méi)有虛函數(shù)的C++類,是不會(huì)有虛函數(shù)表的。

兩張圖:

?

?


簡(jiǎn)單例子:

?

1 #include <iostream>2 #include <windows.h>3 4 using namespace std;5 6 class base7 {8 virtual void f(){cout<<"base::f"<<endl;};9 virtual void g(){cout<<"base::g"<<endl;}; 10 virtual void h(){cout<<"base::h"<<endl;}; 11 }; 12 13 typedef void (*pfun)(); 14 15 void main() 16 { 17 DWORD w=0x4011e0; //虛函數(shù)表第一項(xiàng)的內(nèi)容,也就是第一個(gè)虛函數(shù)的地址 18 19 pfun fun=NULL; 20 base b; 21 base *pbase=&b; 22 23 fun=(pfun)w; 24 fun(); //調(diào)用第一個(gè)虛函數(shù) 25 }

查看對(duì)象b在內(nèi)存中:

查看虛函數(shù)表:

?

?虛函數(shù)表的指針4個(gè)字節(jié)大小(vptr),存在于對(duì)象實(shí)例中最前面的位置(這是為了保證取到虛函數(shù)表的有最高的性能——如果有多層繼承或是多重繼承的情況下)。這意味著我們通過(guò)對(duì)象實(shí)例的地址得到這張?zhí)摵瘮?shù)表,然后就可以遍歷其中函數(shù)指針,并調(diào)用相應(yīng)的函數(shù)。

?

虛函數(shù)表的結(jié)束標(biāo)志在不同的編譯器下是不同的。在VC6.0下,這個(gè)值是NULL,如圖:

?

?


另一個(gè)例子:

?

1 #include <iostream>2 3 using namespace std;4 5 class base6 {7 virtual void f(){cout<<"base::f"<<endl;};8 virtual void g(){cout<<"base::g"<<endl;};9 virtual void h(){cout<<"base::h"<<endl;}; 10 }; 11 12 class Derive : public base 13 { 14 15 public: 16 Derive(){}; 17 virtual void f() { cout << "Derive::f" << endl; } 18 virtual void g() { cout << "Derive::g" << endl; } 19 20 }; 21 22 typedef void(*pfun)(); 23 24 void main() 25 { 26 pfun fun=NULL; 27 Derive d; 28 base *p=&d; 29 30 fun=(pfun)**((int**)p); 31 fun(); //調(diào)用第一個(gè)虛函數(shù) 32 33 fun=(pfun)*(*((int**)p)+2); 34 fun(); //調(diào)用第三個(gè)函數(shù) 35 36 }

?

查看對(duì)象d在內(nèi)存中:

?


多重繼承:

?? 有幾個(gè)父類,就有幾個(gè)vtab和vptr

?

?

?

代碼:

1 #include <iostream>2 3 using namespace std;4 5 class Base1 {6 7 public:8 9 virtual void f() { cout << "Base1::f" << endl; }10 11 virtual void g() { cout << "Base1::g" << endl; }12 13 virtual void h() { cout << "Base1::h" << endl; }14 15 16 17 };18 19 class Base2 {20 21 public:22 23 virtual void f() { cout << "Base2::f" << endl; }24 25 virtual void g() { cout << "Base2::g" << endl; }26 27 virtual void h() { cout << "Base2::h" << endl; }28 29 };30 31 32 class Base3 {33 34 public:35 36 virtual void f() { cout << "Base3::f" << endl; }37 38 virtual void g() { cout << "Base3::g" << endl; }39 40 virtual void h() { cout << "Base3::h" << endl; }41 42 };43 44 45 class Derive : public Base1, public Base2, public Base3 {46 47 public:48 49 virtual void f() { cout << "Derive::f" << endl; }50 51 virtual void g1() { cout << "Derive::g1" << endl; }52 53 };54 55 56 typedef void(*Fun)(void);57 58 int main() 59 60 {61 62 Fun pFun = NULL;63 64 Derive d;65 66 int** pVtab = (int**)&d;67 68 //Base1's vtable69 70 //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+0);71 72 pFun = (Fun)pVtab[0][0];73 74 pFun();75 76 77 //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+1);78 79 pFun = (Fun)pVtab[0][1];80 81 pFun();82 83 84 //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+2);85 86 pFun = (Fun)pVtab[0][2];87 88 pFun();89 90 91 //Derive's vtable92 93 //pFun = (Fun)*((int*)*(int*)((int*)&d+0)+3);94 95 pFun = (Fun)pVtab[0][3];96 97 pFun();98 99 100 //The tail of the vtable 101 102 pFun = (Fun)pVtab[0][4]; 103 104 cout<<pFun<<endl; 105 106 107 //Base2's vtable 108 109 //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+0); 110 111 pFun = (Fun)pVtab[1][0]; 112 113 pFun(); 114 115 116 //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+1); 117 118 pFun = (Fun)pVtab[1][1]; 119 120 pFun(); 121 122 123 pFun = (Fun)pVtab[1][2]; 124 125 pFun(); 126 127 128 //The tail of the vtable 129 130 pFun = (Fun)pVtab[1][3]; 131 132 cout<<pFun<<endl; 133 134 135 //Base3's vtable 136 137 //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+0); 138 139 pFun = (Fun)pVtab[2][0]; 140 141 pFun(); 142 143 144 //pFun = (Fun)*((int*)*(int*)((int*)&d+1)+1); 145 146 pFun = (Fun)pVtab[2][1]; 147 148 pFun(); 149 150 151 pFun = (Fun)pVtab[2][2]; 152 153 pFun(); 154 155 156 //The tail of the vtable 157 158 pFun = (Fun)pVtab[2][3]; 159 160 cout<<pFun<<endl; 161 162 163 cout<<sizeof(d)<<endl; 164 165 return 0; 166 167 } 超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的C++虚函数与虚函数表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。