• <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關鍵字用于取消構造函數的隱式轉換,對有多個參數的構造函數使用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++特性

            導航

            <2006年12月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統計

            常用鏈接

            留言簿(3)

            隨筆分類(29)

            搜索

            最新隨筆

            最新評論

            2021国内久久精品| 中文字幕亚洲综合久久2| 久久线看观看精品香蕉国产| 男女久久久国产一区二区三区| 精品人妻伦一二三区久久| 香蕉久久一区二区不卡无毒影院 | 久久亚洲AV无码精品色午夜麻豆 | 久久777国产线看观看精品| 人妻少妇久久中文字幕| 无码国内精品久久人妻蜜桃| 婷婷久久久亚洲欧洲日产国码AV | 国产精品欧美久久久久天天影视| 成人国内精品久久久久影院| 久久精品无码午夜福利理论片| 久久久久成人精品无码中文字幕 | 久久精品男人影院| 国产精品久久久久无码av| 久久精品国内一区二区三区| 国产高潮国产高潮久久久91 | 日韩欧美亚洲综合久久影院Ds| 午夜精品久久久久9999高清| 久久青青草视频| 久久久久久久亚洲Av无码| 久久综合久久久| 亚洲精品综合久久| 奇米影视7777久久精品| 99热都是精品久久久久久| 怡红院日本一道日本久久| 久久综合久久性久99毛片| 人妻无码αv中文字幕久久| 99久久婷婷国产综合精品草原 | 精品久久久久久中文字幕大豆网| 国产精品免费福利久久| 久久精品国产99久久久香蕉| 久久亚洲私人国产精品vA| 久久久久九国产精品| 久久66热人妻偷产精品9| 久久久午夜精品| 国产真实乱对白精彩久久| 色偷偷88888欧美精品久久久| 久久综合色区|