• <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++編程失樂(lè)園

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

            C++隨筆 之 explicit 關(guān)鍵字(轉(zhuǎn)載)

            explicit關(guān)鍵字用于取消構(gòu)造函數(shù)的隱式轉(zhuǎn)換,對(duì)有多個(gè)參數(shù)的構(gòu)造函數(shù)使用explicit是個(gè)語(yǔ)法錯(cuò)誤。


            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 木木頭 閱讀(420) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++特性

            導(dǎo)航

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

            統(tǒng)計(jì)

            常用鏈接

            留言簿(3)

            隨筆分類(29)

            搜索

            最新隨筆

            最新評(píng)論

            波多野结衣AV无码久久一区| 久久亚洲AV无码精品色午夜麻豆| 久久亚洲国产最新网站| 欧美与黑人午夜性猛交久久久| 亚洲伊人久久综合中文成人网| 中文字幕人妻色偷偷久久 | 国产精品成人久久久久三级午夜电影| 成人久久久观看免费毛片| 国产精品一区二区久久精品涩爱 | 久久综合九色综合网站 | 久久久久av无码免费网| 久久99国产综合精品| 欧美午夜A∨大片久久 | 国产午夜电影久久| 中文字幕日本人妻久久久免费 | 久久亚洲精品中文字幕| 欧美久久一级内射wwwwww.| 97久久精品无码一区二区| 久久久久久亚洲精品影院| 国产精品美女久久久久av爽| 无码超乳爆乳中文字幕久久| 久久天天躁狠狠躁夜夜不卡| 99久久精品国产一区二区| 久久婷婷人人澡人人爽人人爱| 国产成人99久久亚洲综合精品| 久久精品人人做人人爽电影蜜月| 久久无码AV中文出轨人妻| 久久精品视频网| 色偷偷88888欧美精品久久久| 怡红院日本一道日本久久 | 国产V综合V亚洲欧美久久 | 久久AⅤ人妻少妇嫩草影院| 91精品国产综合久久婷婷| 亚洲乱码精品久久久久.. | 99久久夜色精品国产网站| 伊人久久亚洲综合影院| 色婷婷久久久SWAG精品| 一本久道久久综合狠狠躁AV| 久久亚洲国产精品123区| 久久国产成人| 久久久久久午夜精品|