一. 多態(tài)的實(shí)現(xiàn)--虛表
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、富順ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的富順網(wǎng)站制作公司
1.多態(tài)就是多種形態(tài)。在C++中,對(duì)同一父類和子類構(gòu)造出的不同對(duì)象發(fā)出同一條指令,需要它們完成不同的工作,就需要在父類和子類寫出相同函數(shù)名的函數(shù)方法,并用虛函數(shù)區(qū)分它們。父類的指針/引用調(diào)用重寫的虛函數(shù),當(dāng)父類指針/引用指向父類對(duì)象時(shí)調(diào)用的是父類的虛函數(shù),指向子類對(duì)象時(shí)調(diào)用的是子類的虛函數(shù)。
2.虛函數(shù)表是通過(guò)一塊連續(xù)內(nèi)存來(lái)存儲(chǔ)虛函數(shù)的地址。這張表解決了繼承、虛函數(shù)(重寫)的問(wèn)題。在有虛函數(shù)的對(duì)象實(shí)例中都存在一張?zhí)摵瘮?shù)表,虛函數(shù)表就像一張地圖,指明了實(shí)際應(yīng)該調(diào)用的虛函數(shù)函數(shù)。
//帶虛函數(shù)的類 class Base { public: virtual void func1() { cout << "Base::func1()" << endl; } virtual void func2() { cout << "Base::func2()" << endl; } private: int a; }; void Test1() { Base b1; }
3.帶虛函數(shù)的類的模型結(jié)構(gòu)
4.同一類的對(duì)象共用同一虛表
void Test2() { Base b1, b2, b3; }
二. 單繼承&多繼承的對(duì)象模型
1.單繼承:
class Base { public: virtual void func1() { cout << "Base::func1()" << endl; } virtual void func2() { cout << "Base::func2()" << endl; } private: int a; }; class Derive :public Base { public: virtual void func1() { cout << "Derive::func1" << endl; } virtual void func3() { cout << "Derive::func3" << endl; } virtual void func4() { cout << "Derive::func4" << endl; } private: int b; }; typedef void(*FUNC) (); void PrintVTable(int* VTable) { cout << " 虛表地址>" << VTable << endl; for (int i = 0; VTable[i] != 0; ++i) { printf(" 第%d個(gè)虛函數(shù)地址 :0X%x,->", i, VTable[i]); FUNC f = (FUNC)VTable[i]; f(); } cout << endl; } void Test1() { Base b1; Derive d1; int* VTable1 = (int*)(*(int*)&b1); int* VTable2 = (int*)(*(int*)&d1); PrintVTable(VTable1); PrintVTable(VTable2); }
2.多繼承
class Base1 { public: virtual void func1() { cout << "Base1::func1" << endl; } virtual void func2() { cout << "Base1::func2" << endl; } private: int b1; }; class Base2 { public: virtual void func1() { cout << "Base2::func1" << endl; } virtual void func2() { cout << "Base2::func2" << endl; } private: int b2; }; class Derive : public Base1, public Base2 { public: virtual void func1() { cout << "Derive::func1" << endl; } virtual void func3() { cout << "Derive::func3" << endl; } private: int d1; }; typedef void(*FUNC) (); void PrintVTable(int* VTable) { cout << " 虛表地址>" << VTable << endl; for (int i = 0; VTable[i] != 0; ++i) { printf(" 第%d個(gè)虛函數(shù)地址 :0X%x,->", i, VTable[i]); FUNC f = (FUNC)VTable[i]; f(); } cout << endl; } void Test1() { Derive d1; int* VTable = (int*)(*(int*)&d1); PrintVTable(VTable); // Base2虛函數(shù)表在對(duì)象Base1后面 VTable = (int *)(*((int*)&d1 + sizeof(Base1) / 4)); PrintVTable(VTable); }
三. 靜態(tài)多態(tài)和動(dòng)態(tài)多態(tài)
1.靜態(tài)多態(tài)就是重載,因?yàn)槭窃诰幾g期決議確定,所以稱為靜態(tài)多態(tài)。
2.動(dòng)態(tài)多態(tài)就是通過(guò)繼承重寫基類的虛函數(shù)實(shí)現(xiàn)的多態(tài),因?yàn)槭窃谶\(yùn)行時(shí)決議確定,所以稱為動(dòng)態(tài)多態(tài)。
class Base { public: virtual void func1() { cout << "Base::func1" << endl; } virtual void func2() { cout << "Base::func2" << endl; } void display() { cout << "display()" << endl; } void display(int i) { cout << "display(int i)->" << i << endl; } private: int a; }; class Derive :public Base { public: virtual void func1() { cout << "Derive::func1" << endl; } virtual void func3() { cout << "Derive::func3" << endl; } virtual void func4() { cout << "Derive::func4" << endl; } private: int b; }; void func(Base& b) { b.func1(); b.display(); b.display(10); } void Test1() { Base b1; Derive d1; func(b1); func(d1); }
四. 菱形虛擬繼承的對(duì)象模型
class A { public: virtual void FunTest1() { cout << "A::FunTest1()" << endl; } virtual void FunTest2() { cout << "A::FunTest2()" << endl; } private: int _a; }; class B1 :virtual public A { public: virtual void FunTest3() { cout << "B1::FunTest3()" << endl; } virtual void FunTest4() { cout << "B2::FunTest4()" << endl; } private: int _b1; }; class B2 :virtual public A { public: virtual void FunTest1() { cout << "B2::FunTest1()" << endl; } virtual void FunTest4() { cout << "B2::FunTest4()" << endl; } private: int _b2; }; class C :public B1, public B2 { public: virtual void FunTest1() { cout << "C::FunTest1()" << endl; } virtual void FunTest5() { cout << "C::FunTest5()" << endl; } private: int _c; }; void Test1() { C c; c.FunTest5(); }
可見(jiàn),在虛擬繼承中,無(wú)論是幾重繼承,最終子類的虛函數(shù)表總是與繼承列表的第一個(gè)父類的虛函數(shù)表合并在一起,但是有覆蓋的虛函數(shù),自然去覆蓋父類的同名虛函數(shù),并合并在該父類的虛表中。