<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++核心準則C.133:避免保護型數(shù)據(jù)成員?

          dd6a178f78cf3da4a513368618477a09.webp

          9ff0206c1d3b5491c7150bd502a7daa2.webp

          C.133: Avoid protected data

          C.133:避免保護型數(shù)據(jù)成員?

          9ff0206c1d3b5491c7150bd502a7daa2.webp



          7ad6712d89020f273d52ea5836cdc147.webp

          Reason(原因)

          e5917eb009364b111d542486ccd9d120.webp

          protected data is a source of complexity and errors.protected data complicates the statement of invariants.protected data inherently violates the guidance against putting data in base classes, which usually leads to having to deal with virtual inheritance as well.

          保護型數(shù)據(jù)成員是復雜性和錯誤的源頭。保護型數(shù)據(jù)使不變式的表現(xiàn)更加復雜。保護數(shù)據(jù)成員天生違反不要將數(shù)據(jù)放進基類的原則,這通常也會導致必須同時處理虛繼承。


          7ad6712d89020f273d52ea5836cdc147.webp

          Example, bad(反例)

          e5917eb009364b111d542486ccd9d120.webp
          class?Shape?{
          public:
          ????//?...?interface?functions?...
          protected:
          ????//?data?for?use?in?derived?classes:
          ????Color?fill_color;
          ????Color?edge_color;
          ????Style?st;
          };

          Now it is up to every derived Shape to manipulate the protected data correctly. This has been popular, but also a major source of maintenance problems. In a large class hierarchy, the consistent use of protected data is hard to maintain because there can be a lot of code, spread over a lot of classes. The set of classes that can touch that data is open: anyone can derive a new class and start manipulating the protected data. Often, it is not possible to examine the complete set of classes, so any change to the representation of the class becomes infeasible. There is no enforced invariant for the protected data; it is much like a set of global variables. The protected data has de facto become global to a large body of code.

          現(xiàn)在每個派生的Shape類都需要正確地處理保護數(shù)據(jù)。這種做法曾經(jīng)很流行,但也是維護問題的主要來源。在大規(guī)模類層次中,由于代碼量很大而且分散在很多類中,因此使用保護數(shù)據(jù)的一貫性很難維持??梢圆僮鲾?shù)據(jù)的類的集合是開放的:任何人都可以派生出一個新類并操作保護型數(shù)據(jù)。通常無法完全檢查所有這些類,因此對基類表現(xiàn)(數(shù)據(jù)成員,譯者注)的任何修改都變得不可能。不存在針對保護成員的強制性不變量;它們更像一套全局變量。對于大量代碼來說,保護類型數(shù)據(jù)成員事實上已經(jīng)成為全局的了。


          7ad6712d89020f273d52ea5836cdc147.webp

          Note(注意)

          e5917eb009364b111d542486ccd9d120.webp

          Protected data often looks tempting to enable arbitrary improvements through derivation. Often, what you get is unprincipled changes and errors.Prefer private data?with a well-specified and enforced invariant. Alternative, and often better,?keep data out of any class used as an interface.

          保護型數(shù)據(jù)成員經(jīng)??雌饋碓噲D允許通過派生實現(xiàn)自由的改進。通常,你得到的是無規(guī)則的修改和錯誤。更好的選擇是包含良好定義的強制不變量的私有數(shù)據(jù)成員。不將數(shù)據(jù)放進任何作為接口使用的類中通常也是比較好的選擇。


          7ad6712d89020f273d52ea5836cdc147.webp

          Note(注意)

          e5917eb009364b111d542486ccd9d120.webp

          Protected member function can be just fine.

          保護型成員函數(shù)問題不大。


          7ad6712d89020f273d52ea5836cdc147.webp

          Enforcement(實施建議)

          e5917eb009364b111d542486ccd9d120.webp

          Flag classes with protected data.

          提示含有保護型數(shù)據(jù)成員的類。


          7ad6712d89020f273d52ea5836cdc147.webp

          原文鏈接:

          e5917eb009364b111d542486ccd9d120.webp

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c133-avoid-protected-data



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

          關注【面向?qū)ο笏伎肌枯p松學習每一天!

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

          瀏覽 13
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  韩国久久久三级视频 | 玩弄粉嫩护士小泬20p | 抽插综合网 | 亚洲五月丁香影院 | 欧美性爱欧美性爽 |