• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++編程失樂園

            致力于解決論壇的不足,探討C++的原理

            C++隨筆 之 explicit 關鍵字(轉載)

            explicit關鍵字用于取消構造函數(shù)的隱式轉換,對有多個參數(shù)的構造函數(shù)使用explicit是個語法錯誤。


            In C++ it is possible to declare constructors for a class, taking a single parameter, and use those constructors for doing type conversion. For example:

            				
            class ?A?{
            public
            :
            ??????? A(
            int
            );
            };

            void
            ?f(A)?{}
            void
            ?g()
            {
            ???????? A?a1?
            = ? 37
            ;
            ???????? A?a2?
            = ?A( 47
            );
            ???????? A?a3(
            57
            );
            ???????? a1?
            = ? 67
            ;
            ???????? f(
            77
            );
            }

            A declaration like:
            ?A a1 = 37;
            says to call the A(int) constructor to create an A object from the integer value. Such a constructor is called a "converting constructor".

            However, this type of implicit conversion can be confusing, and there is a way of disabling it, using a new keyword "explicit" in the constructor declaration:

            				
            class ?A?{
            public
            :
            ???????
            explicit ?A( int
            );
            };

            void
            ?f(A)?{}
            void
            ?g()
            {
            ????????? A?a1?
            = ? 37 ;?????? // ?illegal

            ????????? A?a2? = ?A( 47 );??? // ?OK
            ????????? A?a3( 57 );??????? // ?OK
            ????????? a1? = ? 67 ;???????? // ?illegal
            ????????? f( 77 );?????????? // ?illegal
            }


            Using the explicit keyword, a constructor is declared to be
            "nonconverting", and explicit constructor syntax is required:

            				
            class ?A?{
            public
            :
            ????????
            explicit ?A( int
            );
            ????????};

            void
            ?f(A)?{}

            void
            ?g()
            {
            ????????A?a1?
            = ?A( 37
            );
            ????????A?a2?
            = ?A( 47
            );
            ????????A?a3(
            57
            );
            ????????a1?
            = ?A( 67
            );
            ????????f(A(
            77
            ));
            }


            Note that an expression such as:

            				        A(47)

            is closely related to function-style casts supported by C++. For example:

            				        double d = 12.34;

            int i = int(d);

            posted on 2007-01-16 13:21 木木頭 閱讀(415) 評論(0)  編輯 收藏 引用 所屬分類: C++特性

            導航

            <2007年1月>
            31123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            統(tǒng)計

            常用鏈接

            留言簿(3)

            隨筆分類(29)

            搜索

            最新隨筆

            最新評論

            国产精品欧美久久久天天影视| 伊人久久亚洲综合影院| 久久99精品综合国产首页| 青青青青久久精品国产| 久久亚洲国产成人精品无码区| 7777精品久久久大香线蕉 | 热久久这里只有精品| 欧美亚洲另类久久综合婷婷| 无码国内精品久久人妻| 久久e热在这里只有国产中文精品99| 久久国产免费直播| 久久99精品九九九久久婷婷| 久久精品国产亚洲AV嫖农村妇女 | 999久久久无码国产精品| 久久久久国产日韩精品网站| 久久青青草原精品国产| 欧美伊人久久大香线蕉综合| 成人国内精品久久久久影院VR| 国产成人精品三上悠亚久久| 久久久久国产精品麻豆AR影院 | 婷婷久久久亚洲欧洲日产国码AV | www性久久久com| 狠狠色丁香婷婷久久综合五月 | 久久久久无码国产精品不卡| 精品国际久久久久999波多野 | 久久国产一区二区| 久久久亚洲欧洲日产国码二区 | 色8久久人人97超碰香蕉987| 国产成人精品久久一区二区三区av| 久久亚洲日韩精品一区二区三区 | 99国产精品久久| 精品无码久久久久国产| 精品少妇人妻av无码久久| 无码伊人66久久大杳蕉网站谷歌| 国产精品久久久久蜜芽| 国内精品久久久久影院亚洲| 伊人久久大香线蕉综合热线| 久久精品极品盛宴观看| 久久久久久久精品妇女99| 久久亚洲精品国产精品| 久久r热这里有精品视频|