<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.90:依靠構(gòu)造函數(shù)和賦值運算符,而不是內(nèi)存初始化和...

          1cd43c2cf72a7ee8b41babfa1602e09f.webp

          青金石

          C.90: Rely on constructors and assignment operators, not memset and memcpy

          C.90:依靠構(gòu)造函數(shù)和賦值運算符,而不是內(nèi)存初始化和內(nèi)存拷貝?


          0110fc30df01f1bfa902ce13e064996d.webp

          Reason(原因)

          0110fc30df01f1bfa902ce13e064996d.webp

          The standard C++ mechanism to construct an instance of a type is to call its constructor. As specified in guideline C.41: a constructor should create a fully initialized object. No additional initialization, such as by memcpy, should be required. A type will provide a copy constructor and/or copy assignment operator to appropriately make a copy of the class, preserving the type's invariants. ?Using memcpy to copy a non-trivially copyable type has undefined behavior. ?Frequently this results in slicing, or data corruption.

          標準C++機制通過調(diào)用構(gòu)造函數(shù)構(gòu)造某個類型的實例。正如C.41說明的:構(gòu)造函數(shù)應(yīng)該生成一個完全初始化的對象。不應(yīng)該要求額外的初始化,例如使用memcpy。類型應(yīng)該提供一個拷貝構(gòu)造函數(shù)和/或者拷貝復制運算符以便適當?shù)厣深惖目截惒⒕S持類的不變量。使用memcpy拷貝一個非平??煽截愵愋偷男袨闆]有定義。通常會導致斷層或者數(shù)據(jù)破壞。


          a17956ed3f0f85adc417c4724754aa42.webp

          Example, good(范例)


          struct?base
          {
          ????virtual?void?update()?=?0;
          ????std::shared_ptr?sp;
          };


          struct?derived?:?public?base
          {
          ????void?update()?override?{}
          };


          89aaa9051eba9949b66a68f0dad7c2e0.webp

          Example, bad(反面示例)


          void?init(derived&?a)
          {
          ????memset(&a,?0,?sizeof(derived));
          }

          This is type-unsafe and overwrites the vtable.

          這個函數(shù)類型不安全而且會覆蓋虛函數(shù)表。


          Example, bad(反面示例)?







          void?copy(derived&?a,?derived&?b)
          {
          ????memcpy(&a,?&b,?sizeof(derived));
          }

          This is also type-unsafe and overwrites the vtable.

          這個函數(shù)同樣是類型不安全而且覆蓋虛函數(shù)表。


          c6d938bdbf404743bb0be41facff1a58.webp7d815f816558abc9d7419b18cc6779c8.webp

          Enforcement(實施建議)?

          265545dad9a55c62649876a2a945eeb4.webp

          • Flag passing a non-trivially-copyable type to memset or memcpy.

          • 提示向memset或memcpy傳遞非平凡可拷貝類型的處理。


          關(guān)于平凡拷貝請參見:

          https://zh.cppreference.com/w/cpp/named_req/TriviallyCopyable?




          a7f53de51804e5be84eb5214f4767e73.webp

          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c90-rely-on-constructors-and-assignment-operators-not-memset-and-memcpy




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

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

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



          瀏覽 67
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  在线免费观看成人网站 | 久久久影院 | 下载小黄片 | 日韩在线视频一区二区三区 | 男人的天堂免费网站 |