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

            亚洲欧美日韩中文久久| 久久99国产一区二区三区| 久久人妻少妇嫩草AV蜜桃| 久久婷婷午色综合夜啪| 国产成人精品综合久久久| 欧美777精品久久久久网| 亚洲精品无码久久毛片| 国产精品久久久久9999高清| 久久久久这里只有精品| 国产精品免费看久久久| 久久这里只有精品视频99| 久久99国产精品尤物| 久久久综合香蕉尹人综合网| 国产V亚洲V天堂无码久久久| 色婷婷综合久久久久中文字幕 | 久久国产亚洲精品无码| 开心久久婷婷综合中文字幕| 女人香蕉久久**毛片精品| 一本一本久久aa综合精品| 日韩精品无码久久一区二区三| 久久精品一区二区三区不卡| 波多野结衣AV无码久久一区| 久久精品国产清自在天天线| 一本色道久久88加勒比—综合| 久久丫精品国产亚洲av不卡| 精品久久久久久中文字幕大豆网 | 婷婷久久香蕉五月综合加勒比| 久久精品无码一区二区三区日韩| 久久97精品久久久久久久不卡 | 久久噜噜久久久精品66| 青青草原综合久久大伊人精品| 欧美午夜精品久久久久免费视| 狠狠色丁香久久婷婷综合_中| 内射无码专区久久亚洲| 无码人妻久久一区二区三区蜜桃| 久久国产精品视频| 国内精品久久久久影院网站| 久久久久亚洲AV成人网人人网站| 精品国产91久久久久久久a| 国产亚洲精久久久久久无码AV| 久久精品国产清自在天天线|