• <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)

            搜索

            最新隨筆

            最新評論

            2019久久久高清456| 综合久久一区二区三区| 色综合久久夜色精品国产| 亚洲成av人片不卡无码久久| 久久人人爽人人爽人人爽| 久久久久久午夜精品| 亚洲国产精品无码久久久蜜芽| 久久人妻AV中文字幕| 中文字幕久久久久人妻| 久久精品国产免费| 热综合一本伊人久久精品| 久久久久久久波多野结衣高潮| 久久精品99久久香蕉国产色戒| 狠狠精品久久久无码中文字幕 | 久久久国产99久久国产一| 久久夜色精品国产欧美乱| 国产ww久久久久久久久久| 国产激情久久久久久熟女老人| 精品国产91久久久久久久| 久久久久国产精品人妻| 91精品久久久久久无码| 亚洲AV无码成人网站久久精品大| 66精品综合久久久久久久| 人妻无码αv中文字幕久久| 久久国产视频网| 久久福利青草精品资源站免费| 亚洲午夜无码久久久久| 久久精品国产亚洲Aⅴ蜜臀色欲| 99久久精品日本一区二区免费| 精品国产乱码久久久久软件| 国产99久久精品一区二区| 久久噜噜久久久精品66| 国产综合精品久久亚洲| 国产精品久久成人影院| av午夜福利一片免费看久久| 青草国产精品久久久久久| 国内高清久久久久久| 一本久久a久久精品亚洲| 久久久久久午夜精品| 欧美伊人久久大香线蕉综合| 精品久久久久久无码不卡|