<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.49:構(gòu)造函數(shù)中應(yīng)該做的是初始化而不是賦值

          4e643ef3efa0f275b9cd4c08a391e9b6.webp

          C.49: Prefer initialization to assignment in constructors
          C.49:構(gòu)造函數(shù)中應(yīng)該做的是初始化而不是賦值

          Reason(原因)

          An initialization explicitly states that initialization, rather than assignment, is done and can be more elegant and efficient. Prevents "use before set" errors.

          初始化明確地表明所做的是初始化而不是賦值,而且可以做得更優(yōu)美,更有效率。防止“賦值之前使用”的錯(cuò)誤。


          Example, good(良好示例)
          class A { ? // Good
          ? ?string s1;
          public:
          ? ?A(czstring p) : s1{p} { } ? ?// GOOD: directly construct (and the C-string is explicitly named)
          ? ?// ...
          };

          Example, bad(反面示例)
          class B { ? // BAD
          ? ?string s1;
          public:
          ? ?B(const char* p) { s1 = p; } ? // BAD: default constructor followed by assignment
          ? ?// ...
          };

          class C { ? // UGLY, aka very bad
          ? ?int* p;
          public:
          ? ?C() { cout << *p; p = new int{10}; } ? // accidental use before initialized
          ? ?// ...
          };

          Example, better still(更好的示例)

          Instead of those const char*s we could use gsl::string_span or (in C++17) std::string_view as a more general way to present arguments to a function:

          相對(duì)于那些const char* s,我們應(yīng)該可以使用gsl::string_span或者(C++17引入的)std::string_view作為表達(dá)函數(shù)參數(shù)怒的更加普遍的方式(https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rstr-view)。

          class D { ? // Good
          ? ?string s1;
          public:
          ? ?A(string_view v) : s1{v} { } ? ?// GOOD: directly construct
          ? ?// ...
          };


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c49-prefer-initialization-to-assignment-in-constructors




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

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

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


          瀏覽 52
          點(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>
                  日韩无码成人片 | 欧美及黄视频在线观看 | 国内精品毛片免费看 | 国内自拍视频网 | 情趣网站在线看。91 |