<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)則ES.63?:不要分割處理對(duì)象

          37a779553399875f935c4352705783ee.webp

          ES.63: Don't slice

          ES.63:不要分割處理對(duì)象


          Reason(原因)

          Slicing -- that is, copying only part of an object using assignment or initialization -- most often leads to errors because the object was meant to be considered as a whole. In the rare cases where the slicing was deliberate the code can be surprising.

          分割指的是在賦值或初始化對(duì)象是只處理對(duì)象一部分--多數(shù)情況下會(huì)導(dǎo)致錯(cuò)誤,因?yàn)閷?duì)象本來(lái)希望作為一個(gè)整體被處理。極少情況下確實(shí)需要分割處理,但是這樣的代碼會(huì)很難理解。


          Example(示例)

          class Shape { /* ... */ };
          class Circle : public Shape { /* ... */ Point c; int r; };

          Circle c {{0, 0}, 42};
          Shape s {c}; // copy construct only the Shape part of Circle
          s = c; // or copy assign only the Shape part of Circle

          void assign(const Shape& src, Shape& dest) {
          dest = src;
          }
          Circle c2 {{1, 1}, 43};
          assign(c, c2); // oops, not the whole state is transferred
          assert(c == c2); // if we supply copying, we should also provide comparison,
          // but this will likely return false

          The result will be meaningless because the center and radius will not be copied from?c?into?s. The first defense against this is to?define the base class?Shape?not to allow this.

          由于中心和半徑不會(huì)從c復(fù)制給s,因此產(chǎn)生沒(méi)有意義的結(jié)果。第一種保護(hù)措施禁止基類的賦值操作。


          Alternative(可選項(xiàng))

          If you mean to slice, define an explicit operation to do so. This saves readers from confusion. For example:

          如果確實(shí)需要分割處理對(duì)象,定義一個(gè)顯式操作完成這個(gè)功能。這樣可以避免讀者困惑。例如:

          class Smiley : public Circle {
          public:
          Circle copy_circle();
          // ...
          };

          Smiley sm { /* ... */ };
          Circle c1 {sm}; // ideally prevented by the definition of Circle
          Circle c2 {sm.copy_circle()};
          Enforcement(實(shí)施建議)

          Warn against slicing.

          發(fā)現(xiàn)分割處理發(fā)出警告。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es63-dont-slice




          覺(jué)得本文有幫助?請(qǐng)分享給更多人。

          關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

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


          瀏覽 47
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          <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>
                  婷婷丁香五月天在线视频 | 神马久久午夜 | 无码人要 | 黄色片网站免费观看 | 久久久久久久久久夜夜想 |