C++核心準(zhǔn)則C.128:虛函數(shù)應(yīng)該明確定義為virtual,overide或者final

C.128: Virtual functions should specify exactly one of virtual, override, or final
C.128:虛函數(shù)應(yīng)該明確定義為virtual,overide或者final

Reason(原因)
Readability. Detection of mistakes. Writing explicit virtual, override, or final is self-documenting and enables the compiler to catch mismatch of types and/or names between base and derived classes. However, writing more than one of these three is both redundant and a potential source of errors.
可讀性。檢出錯(cuò)誤。明確virtual,overide或者final本身就是一種說(shuō)明,而且讓編譯器可以捕捉派生類和基類之間的類型或名稱的不匹配問(wèn)題。然而,使用這三個(gè)中的兩個(gè)或三個(gè)就是多余的而且容易引發(fā)潛在的錯(cuò)誤。
It's simple and clear(下面的規(guī)則簡(jiǎn)單又明快):
virtual means exactly and only "this is a new virtual function."
virual明確表示而且只用于表示"這是一個(gè)新的虛函數(shù)"
override means exactly and only "this is a non-final overrider."
overide明確表示而且只表示“這不是最終覆蓋者”
final means exactly and only "this is a final overrider."
final明確表示而且只用于表示“這是最終覆蓋者”

Example, bad(反面示例)
struct?B?{
????void?f1(int);
????virtual?void?f2(int)?const;
????virtual?void?f3(int);
????//?...
};
struct?D?:?B?{
????void?f1(int);????????//?bad?(hope?for?a?warning):?D::f1()?hides?B::f1()
????void?f2(int)?const;??//?bad?(but?conventional?and?valid):?no?explicit?override
????void?f3(double);?????//?bad?(hope?for?a?warning):?D::f3()?hides?B::f3()
????//?...
};

Example, good(范例)
struct?Better?:?B?{
????void?f1(int)?override;????????//?error?(caught):?Better::f1()?hides?B::f1()
????void?f2(int)?const?override;
????void?f3(double)?override;?????//?error?(caught):?Better::f3()?hides?B::f3()
????//?...
};

Discussion(討論)
We want to eliminate two particular classes of errors:
我們希望排除兩類特殊的錯(cuò)誤:
implicit virtual: the programmer intended the function to be implicitly virtual and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly virtual but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be virtual but it is (because it happens to have the same signature as a virtual in the base class)
隱式虛函數(shù):程序員期待函數(shù)隱式成為虛函數(shù)而且它真的就是了(但是代碼的讀者看不出來(lái));或者程序員期待函數(shù)隱式成為虛函數(shù)但是它不是(例如因?yàn)閰?shù)列表的微小出入);或者程序員沒(méi)有期待函數(shù)成為虛函數(shù)但是它是了(因?yàn)樗∏蓳碛泻突惖哪硞€(gè)虛函數(shù)相同的簽名)
implicit override: the programmer intended the function to be implicitly an overrider and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly an overrider but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be an overrider but it is (because it happens to have the same signature as a virtual in the base class -- note this problem arises whether or not the function is explicitly declared virtual, because the programmer may have intended to create either a new virtual function or a new nonvirtual function)
隱式覆蓋:程序員期待函數(shù)隱式成為覆蓋函數(shù)而且它真的就是了(但是代碼的讀者看不出來(lái));或者程序員期待函數(shù)隱式成為覆蓋函數(shù)但是它不是(例如因?yàn)閰?shù)列表的微小出入);或者程序員沒(méi)有期待函數(shù)成為覆蓋函數(shù)但是它是了(因?yàn)樗∏蓳碛泻突惖哪硞€(gè)虛函數(shù)相同的簽名--注意這些問(wèn)題無(wú)論函數(shù)是否被明確地聲明為虛函數(shù)都會(huì)發(fā)生,因?yàn)槌绦騿T本來(lái)想生成的即可能是虛函數(shù),也可能是非虛函數(shù))

Enforcement(實(shí)施建議)
Compare virtual function names in base and derived classes and flag uses of the same name that does not override.
比較基類和派生類的虛函數(shù)名稱并且提示使用相同名稱但不是override的情況。
Flag overrides with neither override nor final.
提示沒(méi)有聲明為override或者finald的覆蓋函數(shù)。
Flag function declarations that use more than one of virtual, override, and final.
提示使用virtual,override,final三個(gè)關(guān)鍵詞的兩個(gè)或三個(gè)的函數(shù)聲明。

原文鏈接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?br />
