深入理解c++中virtual關(guān)鍵字
1.virtual關(guān)鍵字主要是什么作用?
c++中的函數(shù)調(diào)用默認(rèn)不適用動(dòng)態(tài)綁定。要觸發(fā)動(dòng)態(tài)綁定,必須滿足兩個(gè)條件:第一,指定為虛函數(shù);第二,通過(guò)基類類型的引用或指針調(diào)用。
由此可見(jiàn),virtual主要主要是實(shí)現(xiàn)動(dòng)態(tài)綁定。
2.那些情況下可以使用virtual關(guān)鍵字?
virtual可用來(lái)定義類函數(shù)和應(yīng)用到虛繼承。
友元函數(shù) 構(gòu)造函數(shù) static靜態(tài)函數(shù) 不能用virtual關(guān)鍵字修飾;
普通成員函數(shù) 和析構(gòu)函數(shù) 可以用virtual關(guān)鍵字修飾;
3.virtual函數(shù)的效果
class GrandFather
{
public:
GrandFather() {}
virtual void fun()
{
cout << "GrandFather call function!" << endl;
}
};
class Father : public GrandFather
{
public:
Father() {}
void fun()
{
cout << "Father call function!" << endl;
}
};
class Son : public Father
{
public:
Son() {}
void fun()
{
cout << "Son call function!" << endl;
}
};
void print(GrandFather* father)
{
father->fun();
}
int _tmain(int argc, _TCHAR* argv[])
{
Father * pfather = new Son;
pfather->fun();
GrandFather * pgfather = new Father;
print(pgfather);
return 0;
}
輸出為 Son call function
Father call function
4.virtual的繼承性
只要基函數(shù)定義了virtual,繼承類的該函數(shù)也就具有virtual屬性
即 GrandFather Father Son同時(shí)定義virtual void fun()與GrandFather一個(gè)定義virtual void fun效果是一樣的
5.虛析構(gòu)函數(shù)
class GrandFather
{
public:
GrandFather() {}
virtual void fun()
{
cout << "GrandFather call function!" << endl;
}
~GrandFather()
{
cout << "GrandFather destruction!" << endl;
}
};
class Father : public GrandFather
{
public:
Father() {}
void fun()
{
cout << "Father call function!" << endl;
}
~Father()
{
cout << "Father destruction!" << endl;
}
};
class Son : public Father
{
public:
Son() {}
void fun()
{
cout << "Son call function!" << endl;
}
~Son()
{
cout << "Son destruction!" << endl;
}
};
void print(GrandFather* p)
{
p->fun();
}
int _tmain(int argc, _TCHAR* argv[])
{
Father * pfather = new Son;
delete pfather;
return 0;
}
以上代碼輸出:Father destruction!
GrandFather destruction!
執(zhí)行了Son的構(gòu)造函數(shù),沒(méi)執(zhí)行Son的析構(gòu)函數(shù),故把GrandFather的析構(gòu)函數(shù)設(shè)置為virtual
則輸出: Son destruction!
Father Destruction!
GrandFather destruction!
6. 純虛函數(shù)
純虛函數(shù)定義如下:
class GrandFather
{
public:
GrandFather() {}
virtual void fun() = 0
{
cout << "GrandFather call function!" << endl;
}
virtual ~GrandFather()
{
cout << "GrandFather destruction!" << endl;
}
};
純虛函數(shù)為后代類提供可覆蓋的接口,但這個(gè)類中的版本決不會(huì)調(diào)用。
含有(或繼續(xù))一個(gè)或多個(gè)純虛函數(shù)的類是抽象基類,抽象基類不能實(shí)例化!
繼承類只有重寫這個(gè)接口才能被實(shí)例化
7.虛繼承
虛繼承主要解決交叉繼承帶來(lái)的問(wèn)題。這里給出一片參考文章c++虛繼承。
給一個(gè)例子如下
class GrandFather
{
public:
GrandFather() {}
void fun()
{
cout << "GrandFather call function!" << endl;
}
virtual ~GrandFather()
{
cout << "GrandFather destruction!" << endl;
}
};
class Father1 : public GrandFather
{
public:
Father1() {}
void fun()
{
cout << "Father call function!" << endl;
}
};
class Father2 : public GrandFather
{
public:
Father2() {}
void fun()
{
cout << "Father call function!" << endl;
}
};
class Son : public Father1, public Father2
{
public:
Son() {}
//void fun()
//{
// cout << "Son call function!" << endl;
//}
};
void print(GrandFather* p)
{
p->fun();
}
int _tmain(int argc, _TCHAR* argv[])
{
Son* son = new Son;
son->fun();
return 0;
}
編譯時(shí)會(huì)提示報(bào)錯(cuò)對(duì)fun的訪問(wèn)不明確
如果Father1和Father2都用虛繼承繼承GrandFather類則可以解決這個(gè)問(wèn)題
8. 構(gòu)造函數(shù)和析構(gòu)函數(shù)中的虛函數(shù)
如果在構(gòu)造函數(shù)或析構(gòu)函數(shù)中調(diào)用虛函數(shù),則運(yùn)行的是為構(gòu)造函數(shù)或析構(gòu)函數(shù)自身類型定義的版本
9.虛函數(shù)的實(shí)現(xiàn)機(jī)制
關(guān)于虛函數(shù)的實(shí)現(xiàn)機(jī)制,我們以后在介紹。
10.小結(jié)
關(guān)于virtual關(guān)鍵字的用法總結(jié)如上,有錯(cuò)誤或者總結(jié)不到位的情況請(qǐng)能幫本人指出!
11.例子
class classA
{
public:
classA()
{
clear();
}
virtual ~classA()
{
}
void clear()
{
memset(this , 0 , sizeof(*this));
}
virtual void func()
{
printf("func\n");
}
};
class classB : public classA
{
};
int main(void)
{
classA oa;
classB ob;
classA * pa0 = &oa;
classA * pa1 = &ob;
classB * pb = &ob;
oa.func(); // 1
ob.func(); // 2
pa0->func(); // 3
pa1->func(); // 4
pb->func(); // 5
return 0;
}
補(bǔ)充一個(gè)例子,這個(gè)程序輸出依次是
func
func
出錯(cuò)
func
func
談?wù)勎业睦斫猓?dāng)
classA oa;
oa.func();
不存在動(dòng)態(tài)調(diào)用的過(guò)程,所以func雖然是虛函數(shù),但是函數(shù)調(diào)用不通過(guò)虛表訪問(wèn),所以即使
memset(this , 0 , sizeof(*this));
找不到虛表地址也沒(méi)有關(guān)系
在執(zhí)行classB ob;的時(shí)候,注意memset的是classA的地址,所有ob的虛表是存在的
即是如下,通過(guò)指針或引用(動(dòng)態(tài)綁定)訪問(wèn)oa的func函數(shù)(需要從虛表訪問(wèn)),會(huì)出錯(cuò)
訪問(wèn)ob的func和函數(shù),無(wú)論靜態(tài)訪問(wèn)還是動(dòng)態(tài)訪問(wèn),都不會(huì)出錯(cuò)
當(dāng)把classB的代碼改成如下時(shí)
class classB : public classA
<PRE style="FONT-WEIGHT: bold" class=cpp name="code">{</PRE><PRE style="FONT-WEIGHT: bold" class=cpp name="code"> classB()
{
clear();
}
virtual ~classB()
{
}
void clear()
{
memset(this , 0 , sizeof(*this));
}</PRE><BR>
<PRE></PRE>
<PRE style="FONT-WEIGHT: bold" class=cpp name="code">};</PRE>輸出為
func
func
出錯(cuò)
出錯(cuò)
出錯(cuò)
相關(guān)文章
typedef_struct與struct之間的區(qū)別
本篇文章主要是對(duì)typedef struct與struct之間的區(qū)別進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
如何在C語(yǔ)言中提取Shellcode并執(zhí)行
Shellcode是一種獨(dú)立于應(yīng)用程序的機(jī)器代碼,通常用于實(shí)現(xiàn)特定任務(wù),如執(zhí)行遠(yuǎn)程命令、注入惡意軟件或利用系統(tǒng)漏洞,本文將深入探討如何在C語(yǔ)言中提取Shellcode,并通過(guò)XOR加密技術(shù)增加其混淆程度,文中通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2023-12-12
C++使用HTTP庫(kù)和框架輕松發(fā)送HTTP請(qǐng)求
使用C++編程發(fā)送HTTP請(qǐng)求通常需要使用第三方的HTTP庫(kù)或框架,本文主要介紹了C++使用HTTP庫(kù)和框架輕松發(fā)送HTTP請(qǐng)求,感興趣的可以了解一下2023-12-12
Opencv開(kāi)發(fā)實(shí)現(xiàn)拼圖游戲
這篇文章主要為大家詳細(xì)介紹了Opencv開(kāi)發(fā)實(shí)現(xiàn)拼圖游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07

