<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.131: 避免無意義的getters和setters?

          e16ae9c5386804e64071d593bef64147.webp



          C.131: Avoid trivial getters and setters

          C.131: 避免無意義的getters和setters?




          Reason(原因)




          A trivial getter or setter adds no semantic value; the data item could just as well be public.

          無意義的getter和setter不會增加任何語義上的價值,數(shù)據(jù)項只要定義為public就好。


          Example(示例)




          class?Point?{???//?Bad:?verbose
          ????int?x;
          ????int?y;
          public:
          ????Point(int?xx,?int?yy)?:?x{xx},?y{yy}?{?}
          ????int?get_x()?const?{?return?x;?}
          ????void?set_x(int?xx)?{?x?=?xx;?}
          ????int?get_y()?const?{?return?y;?}
          ????void?set_y(int?yy)?{?y?=?yy;?}
          ????//?no?behavioral?member?functions
          };

          Consider making such a class a struct -- that is, a behaviorless bunch of variables, all public data and no member functions.

          考慮將這樣的類定義為struct--也就是說,不包含行為的數(shù)據(jù)群,所有數(shù)據(jù)都公開,沒有成員函數(shù)。

          struct?Point?{
          ????int?x?{0};
          ????int?y?{0};
          };

          Note that we can put default initializers on member variables:?C.49: Prefer initialization to assignment in constructors.

          注意我們可以為成員變量設(shè)置初始化器:C.49:初始化比在構(gòu)造函數(shù)中復(fù)制更好。


          Note(注意)




          The key to this rule is whether the semantics of the getter/setter are trivial. While it is not a complete definition of "trivial", consider whether there would be any difference beyond syntax if the getter/setter was a public data member instead. Examples of non-trivial semantics would be: maintaining a class invariant or converting between an internal type and an interface type.

          這條準(zhǔn)則的關(guān)鍵是getter/setter的語義是不是有意義。如果不能完全定義“無意義”,考慮如果getter/setter是公有成員的話是否存在任何的不同。有意義的語義的示例:維持類的不變量或者在內(nèi)部數(shù)據(jù)類型和接口數(shù)據(jù)類型之間進(jìn)行的轉(zhuǎn)換。


          Enforcement(實施建議)




          Flag multiple get and set member functions that simply access a member without additional semantics.

          如果存在多個get和set成員函數(shù),只是簡單地訪問數(shù)據(jù)成員卻不包含附加意義,進(jìn)行提示。


          原文鏈接:




          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c130-for-making-deep-copies-of-polymorphic-classes-prefer-a-virtual-clone-function-instead-of-copy-constructionassignment




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

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

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

          瀏覽 63
          點贊
          評論
          收藏
          分享

          手機(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>
                  色逼123 | 在线观看黄色视频网站 | 日韩操逼| 超碰99在线免费观看 | 国产足交免费看 |