<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.137: 使用虛基類避免過于一般的基類?

          c54e35415af2f50d0cf1d411d1e43534.webp

          C.137: Use?virtual?bases to avoid overly general base classes

          C.137: 使用虛基類避免過于一般的基類?



          Reason(原因)







          Allow separation of shared data and interface. To avoid all shared data to being put into an ultimate base class.

          允許共享數(shù)據(jù)和接口的分離。避免將所有的共享數(shù)據(jù)放進(jìn)一個終極基類中。


          Example(示例)







          struct?Interface?{
          ????virtual?void?f();
          ????virtual?int?g();
          ????//?...?no?data?here?...
          };

          class?Utility?{??//?with?data
          ????void?utility1();
          ????virtual?void?utility2();????//?customization?point
          public:
          ????int?x;
          ????int?y;
          };

          class?Derive1?:?public?Interface,?virtual?protected?Utility?{
          ????//?override?Interface?functions
          ????//?Maybe?override?Utility?virtual?functions
          ????//?...
          };

          class?Derive2?:?public?Interface,?virtual?protected?Utility?{
          ????//?override?Interface?functions
          ????//?Maybe?override?Utility?virtual?functions
          ????//?...
          };

          Factoring out?Utility?makes sense if many derived classes share significant "implementation details."

          如果很多派生類之間分享特別有用的共通的"實(shí)現(xiàn)細(xì)節(jié)",從中分離出共通功能就是有意義的。


          Note(注意)







          Obviously, the example is too "theoretical", but it is hard to find a?small?realistic example.?Interface?is the root of an?interface hierarchy?and?Utility?is the root of an?implementation hierarchy. Here is?a slightly more realistic example?with an explanation.

          很顯然,示例過于理論化了,但是找到一個接近現(xiàn)實(shí)的小例子太難了。接口是接口體系的起點(diǎn),而公用程序是實(shí)現(xiàn)體系的起點(diǎn)。這里有一個帶有說明的,略微更接近實(shí)際的例子。

          鏈接:https://www.quora.com/What-are-the-uses-and-advantages-of-virtual-base-class-in-C%2B%2B/answer/Lance-Diduck


          Note(注意)







          Often, linearization of a hierarchy is a better solution.

          通常,線性的繼承體系是較好的解決方案。


          Enforcement(實(shí)施建議)







          Flag mixed interface and implementation hierarchies.

          提示接口繼承和實(shí)現(xiàn)繼承體系混合的情況。


          原文鏈接






          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c137-use-virtual-bases-to-avoid-overly-general-base-classes




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

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

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

          瀏覽 35
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  日韩中文字幕熟妇人妻 | 高清一区二区三区 | 色老板成人无码版在线播放 | 欧美日韩小电影 | 激情四房婷婷 |