C++11?中的override詳解
1 公有繼承
公有繼承包含兩部分:一是"函數(shù)接口" (interface),二是"函數(shù)實(shí)現(xiàn)" (implementation)
如 Shape 類(lèi)中,三個(gè)成員函數(shù),對(duì)應(yīng)三種繼承方式:
class Shape {
public:
virtual void Draw() const = 0; // 1) 純虛函數(shù)
virtual void Error(const string& msg); // 2) 普通虛函數(shù)
int ObjectID() const; // 3) 非虛函數(shù)
};
class Rectangle: public Shape { ... };
class Ellipse: public Shape { ... }; 1.1 純虛函數(shù) (pure virtual)
Shape *ps1 = new Rectangle; ps1->Draw(); // calls Rectangle::Draw Shape *ps2 = new Ellipse; ps2->Draw(); // calls Ellipse::Draw
純虛函數(shù),繼承的是基類(lèi)中,成員函數(shù)的接口,且要在派生類(lèi)中,重寫(xiě)成員函數(shù)的實(shí)現(xiàn)
調(diào)用基類(lèi)的 Draw(),須加 類(lèi)作用域操作符 ::
ps1->Shape::Draw(); // calls Shape::draw
1.2 普通虛函數(shù)
普通虛函數(shù),會(huì)在基類(lèi)中,定義一個(gè)缺省的實(shí)現(xiàn) (default implementation),表示繼承的是基類(lèi)成員函數(shù)接口和缺省實(shí)現(xiàn),由派生類(lèi)選擇是否重寫(xiě)該函數(shù)。
實(shí)際上,允許普通虛函數(shù) 同時(shí)繼承接口和缺省實(shí)現(xiàn)是危險(xiǎn)的。如下, ModelA 和 ModelB 是 Airplane 的兩種飛機(jī)類(lèi)型,且二者的飛行方式完全相同
class Airplane {
public:
virtual void Fly(const string& destination);
};
class ModelA: public Airplane { ... };
class ModelB: public Airplane { ... }; 這是典型的面向?qū)ο笤O(shè)計(jì),兩個(gè)類(lèi)共享一個(gè)特性 -- Fly,則 Fly 可在基類(lèi)中實(shí)現(xiàn),并由兩個(gè)派生類(lèi)繼承之
現(xiàn)增加另一個(gè)飛機(jī)型號(hào) ModelC,其飛行方式與 ModelA,ModelB 不相同,如果不小心忘記在 ModelC 中重寫(xiě)新的 Fly 函數(shù)
class ModelC: public Airplane {
... // no fly function is declared
}; 則調(diào)用 ModelC 中的 fly 函數(shù),就是調(diào)用 Airplane::Fly,但是 ModelC 的飛行方式和缺省的并不相同
Airplane *pa = new ModelC; pa->Fly(Qingdao); // calls Airplane::fly!
即前面所說(shuō)的,普通虛函數(shù)同時(shí)繼承接口和缺省實(shí)現(xiàn)是危險(xiǎn)的,最好是基類(lèi)中實(shí)現(xiàn)缺省行為 (behavior),但只有在派生類(lèi)要求時(shí)才提供該缺省行為
1.2.1 方法一
一種方法是 純虛函數(shù) + 缺省實(shí)現(xiàn),因?yàn)槭羌兲摵瘮?shù),所以只有接口被繼承,其缺省的實(shí)現(xiàn)不會(huì)被繼承。派生類(lèi)要想使用該缺省的實(shí)現(xiàn),必須顯式的調(diào)用
class Airplane {
public:
virtual void Fly(const string& destination) = 0;
};
void Airplane::Fly(const string& destination)
{
// a pure virtual function default code for flying an airplane to the given destination
}
class ModelA: public Airplane {
virtual void Fly(const string& destination) { Airplane::Fly(destination); }
}; 這樣在派生類(lèi) ModelC 中,即使一不小心忘記重寫(xiě) Fly 函數(shù),也不會(huì)調(diào)用 Airplane 的缺省實(shí)現(xiàn)
class ModelC: public Airplane {
public:
virtual void Fly(const string& destination);
};
void ModelC::Fly(const string& destination)
{
// code for flying a ModelC airplane to the given destination
} 1.2.2 方法二
可以看到,上面問(wèn)題的關(guān)鍵就在于,一不小心在派生類(lèi) ModelC 中忘記重寫(xiě) fly 函數(shù),C++11 中使用關(guān)鍵字 override,可以避免這樣的“一不小心”
1.3 非虛函數(shù)
非虛成員函數(shù)沒(méi)有 virtual 關(guān)鍵字,表示派生類(lèi)不但繼承了接口,而且繼承了一個(gè)強(qiáng)制實(shí)現(xiàn) (mandatory implementation)
既然繼承了一個(gè)強(qiáng)制的實(shí)現(xiàn),則在派生類(lèi)中,無(wú)須重新定義 (redefine) 繼承自基類(lèi)的成員函數(shù),如下:
使用指針調(diào)用 ObjectID 函數(shù),則都是調(diào)用的 Shape::ObjectID()
Rectangel rc; // rc is an object of type Rectangle Shape *pB = &rc; // get pointer to rc pB->ObjectID(); // call ObjectID() through pointer Rectangle *pD = &rc; // get pointer to rc pD->ObjectID(); // call ObjectID() through pointer
如果在派生類(lèi)中重新定義了繼承自基類(lèi)的成員函數(shù) ObjectID 呢?
class Rectangel : public Shape {
public:
int ObjectID() const; // hides Shape::ObjectID
};
pB->ObjectID(); // calls Shape::ObjectID()
pD->ObjectID(); // calls Rectagle::ObjectID() 此時(shí),派生類(lèi)中重新定義的成員函數(shù)會(huì) “隱藏” (hide) 繼承自基類(lèi)的成員函數(shù)
這是因?yàn)榉翘摵瘮?shù)是 “靜態(tài)綁定” 的,pB 被聲明的是 Shape* 類(lèi)型的指針,則通過(guò) pB 調(diào)用的非虛函數(shù)都是基類(lèi)中的,既使 pB 指向的是派生類(lèi)
與“靜態(tài)綁定”相對(duì)的是虛函數(shù)的“動(dòng)態(tài)綁定”,即無(wú)論 pB 被聲明為 Shape* 還是 Rectangle* 類(lèi)型,其調(diào)用的虛函數(shù)取決于 pB 實(shí)際指向的對(duì)象類(lèi)型
2 重寫(xiě) (override)
在 1.2.2 中提到 override 關(guān)鍵字,可以避免派生類(lèi)中忘記重寫(xiě)虛函數(shù)的錯(cuò)誤
下面以重寫(xiě)虛函數(shù)時(shí),容易犯的四個(gè)錯(cuò)誤為例,詳細(xì)闡述之
class Base {
public:
virtual void mf1() const;
virtual void mf2(int x);
virtual void mf3() &;
void mf4() const; // is not declared virtual in Base
};
class Derived: public Base {
public:
virtual void mf1(); // declared const in Base, but not in Derived.
virtual void mf2(unsigned int x); // takes an int in Base, but an unsigned int in Derived
virtual void mf3() &&; // is lvalue-qualified in Base, but rvalue-qualified in Derived.
void mf4() const;
}; 在派生類(lèi)中,重寫(xiě) (override) 繼承自基類(lèi)成員函數(shù)的實(shí)現(xiàn) (implementation) 時(shí),要滿足如下條件:
一虛:基類(lèi)中,成員函數(shù)聲明為虛擬的 (virtual)
二容:基類(lèi)和派生類(lèi)中,成員函數(shù)的返回類(lèi)型和異常規(guī)格 (exception specification) 必須兼容
四同:基類(lèi)和派生類(lèi)中,成員函數(shù)名、形參類(lèi)型、常量屬性 (constness) 和 引用限定符 (reference qualifier) 必須完全相同
如此多的限制條件,導(dǎo)致了虛函數(shù)重寫(xiě)如上述代碼,極容易因?yàn)橐粋€(gè)不小心而出錯(cuò)
C++11 中的 override 關(guān)鍵字,可以顯式的在派生類(lèi)中聲明,哪些成員函數(shù)需要被重寫(xiě),如果沒(méi)被重寫(xiě),則編譯器會(huì)報(bào)錯(cuò)。
class Derived: public Base {
public:
virtual void mf1() override;
virtual void mf2(unsigned int x) override;
virtual void mf3() && override;
virtual void mf4() const override;
}; 因此,即使不小心漏寫(xiě)了虛函數(shù)重寫(xiě)的某個(gè)苛刻條件,也可以通過(guò)編譯器的報(bào)錯(cuò),快速改正錯(cuò)誤
class Derived: public Base {
public:
virtual void mf1() const override; // adding "virtual" is OK, but not necessary
virtual void mf2(int x) override;
void mf3() & override;
void mf4() const override;
}; 小結(jié):
1) 公有繼承
純虛函數(shù) => 繼承的是:接口 (interface)
普通虛函數(shù) => 繼承的是:接口 + 缺省實(shí)現(xiàn) (default implementation)
非虛成員函數(shù) =>繼承的是:接口 + 強(qiáng)制實(shí)現(xiàn) (mandatory implementation)
2) 不要重新定義一個(gè)繼承自基類(lèi)的非虛函數(shù) (never redefine an inherited non-virtual function)
3) 在聲明需要重寫(xiě)的函數(shù)后,加關(guān)鍵字 override
參考資料
《Effective C++》3rd,item 34, item 36
《Effective Modern C++》 item 12
到此這篇關(guān)于C++11 中的override詳解的文章就介紹到這了,更多相關(guān)C++11 override內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于C語(yǔ)言利用哈夫曼樹(shù)實(shí)現(xiàn)文件壓縮的問(wèn)題
哈夫曼編碼是一種編碼方式,又稱“霍夫曼編碼”,其是可變字長(zhǎng)的編碼(VCL)的一種,這篇文章主要介紹了基于C語(yǔ)言利用哈夫曼樹(shù)實(shí)現(xiàn)文件壓縮,需要的朋友可以參考下2021-08-08
C 語(yǔ)言基礎(chǔ)教程(我的C之旅開(kāi)始了)[四]
C 語(yǔ)言基礎(chǔ)教程(我的C之旅開(kāi)始了)[四]...2007-02-02
詳解C++中的const關(guān)鍵字及與C語(yǔ)言中const的區(qū)別
這篇文章主要介紹了C++中的const關(guān)鍵字及與C語(yǔ)言中const的區(qū)別,const將所修飾的變量對(duì)象轉(zhuǎn)化為常量,需要的朋友可以參考下2016-04-04
C語(yǔ)言實(shí)現(xiàn)自行車(chē)存放管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)自行車(chē)存放管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
適合初學(xué)者的C語(yǔ)言數(shù)據(jù)類(lèi)型的講解
在 C 語(yǔ)言中,數(shù)據(jù)類(lèi)型指的是用于聲明不同類(lèi)型的變量或函數(shù)的一個(gè)廣泛的系統(tǒng)。變量的類(lèi)型決定了變量存儲(chǔ)占用的空間,以及如何解釋存儲(chǔ)的位模式。2022-04-04
一篇文章教你用C語(yǔ)言模擬實(shí)現(xiàn)字符串函數(shù)
這篇文章主要介紹了C語(yǔ)言模擬實(shí)現(xiàn)字符串函數(shù),開(kāi)發(fā)程序的時(shí)候經(jīng)常使用到一些字符串函數(shù),例如求字符串長(zhǎng)度,拷貝字符串……,需要的朋友可以參考下2021-09-09
解析C++哈夫曼樹(shù)編碼和譯碼的實(shí)現(xiàn)
本篇文章主要介紹了C++哈夫曼樹(shù)編碼和譯碼的實(shí)現(xiàn),詳細(xì)的講訴了哈夫曼樹(shù)編碼的原理,有需要的同學(xué)可以了解一下。2016-11-11

