<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.46:默認狀態(tài)下明確定義單參數(shù)構造函數(shù)

          共 1508字,需瀏覽 4分鐘

           ·

          2020-01-01 23:23

          86edbb99b3b2e73fc556b27a60bd02f4.webp

          28a441f3a12fb290ad49b1d2de943b74.webpC.46: By default, declare single-argument constructors explicitC.46:默認狀態(tài)下明確定義單參數(shù)構造函數(shù)28a441f3a12fb290ad49b1d2de943b74.webp

          73ae6ca43e3401ba4f6230525ab62f8e.webpReason(原因)73ae6ca43e3401ba4f6230525ab62f8e.webp

          To avoid unintended conversions.

          避免意外的轉換。


          73ae6ca43e3401ba4f6230525ab62f8e.webpExample, bad(反面示例)73ae6ca43e3401ba4f6230525ab62f8e.webp
          class String {
          public:
          ? ?String(int); ? // BAD
          ? ?// ...
          };

          String s = 10; ? // surprise: string of size 10

          73ae6ca43e3401ba4f6230525ab62f8e.webpException(例外)73ae6ca43e3401ba4f6230525ab62f8e.webp

          If you really want an implicit conversion from the constructor argument type to the class type, don't use explicit:

          如果你確實需要一個從構造函數(shù)參數(shù)象類類型的隱式類型轉換,不用使用explicit關鍵字。

          class Complex {
          public:
          ? ?Complex(double d); ? // OK: we want a conversion from d to {d, 0}
          ? ?// ...
          };

          Complex z = 10.7; ? // unsurprising conversion

          See also: Discussion of implicit conversions

          參見:關于隱式類型轉換的討論。

          隱式類型轉換的討論:

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Ro-conversion


          73ae6ca43e3401ba4f6230525ab62f8e.webpException(例外)73ae6ca43e3401ba4f6230525ab62f8e.webp

          Copy and move constructors should not be made explicit because they do not perform conversions. Explicit copy/move constructors make passing and returning by value difficult.

          拷貝和移動構造函數(shù)不應該定義為explicit,應為那樣就不會執(zhí)行類型轉換了。顯式拷貝/移動構造函數(shù)使通過值傳遞參數(shù)和返回結果變得困難。


          73ae6ca43e3401ba4f6230525ab62f8e.webpEnforcement(實施建議)73ae6ca43e3401ba4f6230525ab62f8e.webp

          (Simple) Single-argument constructors should be declared explicit. Good single argument non-explicit constructors are rare in most code bases. Warn for all that are not on a "positive list".

          (簡單)唯一參數(shù)的構造函數(shù)應該被定義為explicit。定義良好的非explicit單參數(shù)構造函數(shù)在大多數(shù)代碼中很少見。對于所有不在“正面清單”中的情況進行警告。

          73ae6ca43e3401ba4f6230525ab62f8e.webp原文鏈接73ae6ca43e3401ba4f6230525ab62f8e.webp

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c46-by-default-declare-single-argument-constructors-explicit



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

          關注【面向對象思考】輕松學習每一天!

          面向對象開發(fā),面向對象思考!

          瀏覽 32
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  水多多www视频在线观看高清 | 婷婷丁香五月在线 | 欧美成人做爱视频 | 粉嫩小泬BBBBBB免费 | 一本道无码在线播放 |