<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.22:保持默認操作的一貫性

          4e12dd0a634605aa50607fb7b006fc08.webp

          C.22: Make default operations consistent

          C.22:?保持默認操作的一貫性


          Reason(原因)

          The default operations are conceptually a matched set. Their semantics are interrelated. Users will be surprised if copy/move construction and copy/move assignment do logically different things. Users will be surprised if constructors and destructors do not provide a consistent view of resource management. Users will be surprised if copy and move don't reflect the way constructors and destructors work.

          默認操作從概念上講是配合嚴密的一整套處理。它們的語義是相互關(guān)聯(lián)的。如果拷貝/移動構(gòu)造和拷貝/移動賦值做的是邏輯上不同的事情,用戶會感到詫異;如果構(gòu)造函數(shù)和析構(gòu)函數(shù)沒有為資源管理提供一致的想法,用戶會感到詫異;如果拷貝和移動操作沒有對應(yīng)構(gòu)造函數(shù)和析構(gòu)函數(shù)的動作,用戶會感到詫異。


          Example, bad(反面示例)
          class Silly {   // BAD: Inconsistent copy operations
          class Impl {
          // ...
          };
          shared_ptr p;
          public:
          Silly(const Silly& a) : p{a.p} { *p = *a.p; } // deep copy
          Silly& operator=(const Silly& a) { p = a.p; } // shallow copy
          // ...
          };

          These operations disagree about copy semantics. This will lead to confusion and bugs.

          這些操作(拷貝構(gòu)造和賦值)關(guān)于拷貝的語義不同(風(fēng)別是深拷貝和淺拷貝)。這會導(dǎo)致困惑和錯誤。


          Enforcement(實施建議)
          • (Complex) A copy/move constructor and the corresponding copy/move assignment operator should write to the same member variables at the same level of dereference.

            (復(fù)雜) 拷貝/移動構(gòu)造函數(shù)和對應(yīng)的拷貝/移動賦值運算符應(yīng)該以同樣的的解引用級別寫入同樣的成員變量。

          • (Complex) Any member variables written in a copy/move constructor should also be initialized by all other constructors.

            (復(fù)雜)在拷貝/移動構(gòu)造函數(shù)中寫入的任何成員變量也應(yīng)該被其他的構(gòu)造函數(shù)初始化。

          • (Complex) If a copy/move constructor performs a deep copy of a member variable, then the destructor should modify the member variable.

            (復(fù)雜)如果拷貝/移動構(gòu)造函數(shù)對成員變量進行深拷貝,那么析構(gòu)函數(shù)應(yīng)該修改該成員變量。

          • (Complex) If a destructor is modifying a member variable, that member variable should be written in any copy/move constructors or assignment operators.

          • (復(fù)雜)如果析構(gòu)函數(shù)修改某個成員變量,那么這個成員變量應(yīng)該在拷貝/移動構(gòu)造函數(shù)或者賦值運算符中被寫入。


          原文鏈接:

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c22-make-default-operations-consistent




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

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

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




          瀏覽 44
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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国产v | 欧美视频中文字幕 | 国产成人性爱 |