明確虛函數的意圖

明確虛函數的意圖

C.128: Virtual functions should specify exactly one of virtual, override, or final

C.128:虛函數應該明確定義為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.

可讀性。檢出錯誤。明確virtual,overide或者final本身就是一種說明,而且讓編譯器可以捕捉派生類和基類之間的類型或名稱的不匹配問題。然而,使用這三個中的兩個或三個就是多餘的而且容易引發潛在的錯誤。

It's simple and clear(下面的規則簡單又明快):

  • virtual means exactly and only "this is a new virtual function."
  • virual明確表示而且只用於表示"這是一個新的虛函數"
  • 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(反面示例)

structB{      voidf1(int);      virtualvoidf2(int)const;      virtualvoidf3(int);      //...};structD:B{      voidf1(int);//bad(hopeforawarning):D::f1()hidesB::f1()      voidf2(int)const;//bad(butconventionalandvalid):noexplicitoverride      voidf3(double);//bad(hopeforawarning):D::f3()hidesB::f3()      //...};

Example, good(範例)

structBetter:B{      voidf1(int)override;//error(caught):Better::f1()hidesB::f1()      voidf2(int)constoverride;      voidf3(double)override;//error(caught):Better::f3()hidesB::f3()      //...};

Discussion(討論)

We want to eliminate two particular classes of errors:

我們希望排除兩類特殊的錯誤:

  • 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)
  • 隱式虛函數:程序員期待函數隱式成為虛函數而且它真的就是了(但是代碼的讀者看不出來);或者程序員期待函數隱式成為虛函數但是它不是(例如因為參數列表的微小出入);或者程序員沒有期待函數成為虛函數但是它是了(因為它恰巧擁有和基類的某個虛函數相同的簽名)
  • 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)
  • 隱式覆蓋:程序員期待函數隱式成為覆蓋函數而且它真的就是了(但是代碼的讀者看不出來);或者程序員期待函數隱式成為覆蓋函數但是它不是(例如因為參數列表的微小出入);或者程序員沒有期待函數成為覆蓋函數但是它是了(因為它恰巧擁有和基類的某個虛函數相同的簽名--注意這些問題無論函數是否被明確地聲明為虛函數都會發生,因為程序員本來想生成的即可能是虛函數,也可能是非虛函數)

Enforcement(實施建議)

  • Compare virtual function names in base and derived classes and flag uses of the same name that does not override.
  • 比較基類和派生類的虛函數名稱並且提示使用相同名稱但不是override的情況。
  • Flag overrides with neither override nor final.
  • 提示沒有聲明為override或者finald的覆蓋函數。
  • Flag function declarations that use more than one of virtual, override, and final.
  • 提示使用virtual,override,final三個關鍵詞的兩個或三個的函數聲明。

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final


覺得本文有幫助?請分享給更多人。

更多精彩文章請關注微信公眾號【面向對象思考】!

面向對象開發,面向對象思考!


分享到:


相關文章: