<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準(zhǔn)則C.121:如果基類被用來定義接口,保證它是一個純虛類?

          ba48ffe4d0671ab506d214fc51a319fa.webp

          綠色碧璽


          C.121: If a base class is used as an interface, make it a pure abstract class

          C.121:如果基類被用來定義接口,保證它是一個純虛類?






          Reason(原因)




          A class is more stable (less brittle) if it does not contain data. Interfaces should normally be composed entirely of public pure virtual functions and a default/empty virtual destructor.

          不包含數(shù)據(jù)的類會更穩(wěn)定(更少脆弱性)。接口通常應(yīng)該由公開的純虛函數(shù)和默認/空的純虛析構(gòu)函數(shù)組成。





          Example(示例)




          class?My_interface?{
          public:
          ????//?...only?pure?virtual?functions?here?...
          ????virtual?~My_interface()?{}???//?or?=default
          };



          Example, bad(反面示例)




          class?Goof?{
          public:
          ????//?...only?pure?virtual?functions?here?...
          ????//?no?virtual?destructor
          };

          class?Derived?:?public?Goof?{
          ????string?s;
          ????//?...
          };

          void?use()
          {
          ????unique_ptr?p?{new?Derived{"here?we?go"}};
          ????f(p.get());?//?use?Derived?through?the?Goof?interface
          ????g(p.get());?//?use?Derived?through?the?Goof?interface
          }?//?leak

          The Derived is deleted through its Goof interface, so its string is leaked. Give Goof a virtual destructor and all is well.

          派生類通過它的Goof接口被銷毀,(但是由于Goof的析構(gòu)函數(shù)不是虛函數(shù),導(dǎo)致Derived的析構(gòu)函數(shù)不會被調(diào)用,譯者注)因此它的string成員會發(fā)生泄露。為Goof設(shè)計一個虛析構(gòu)函數(shù)就所有都OK了。





          Enforcement(實施建議)




          • Warn on any class that contains data members and also has an overridable (non-final) virtual function that wasn't inherited from a base class.

          • 對于所有同時包含數(shù)據(jù)成員和并非繼承自基類的可覆蓋(非最終)虛函數(shù)的類發(fā)出警告。




          原文鏈接:




          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c121-if-a-base-class-is-used-as-an-interface-make-it-a-pure-abstract-class



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

          關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/p>

          瀏覽 48
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  黄色电影一级A片 | 影音先锋自拍视频 | 亲子乱婬A片日韩精品一区 | 51国产精品 | 超碰综合在线 |