• <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 關(guān)鍵字(轉(zhuǎn)載)

            explicit關(guān)鍵字用于取消構(gòu)造函數(shù)的隱式轉(zhuǎn)換,對有多個參數(shù)的構(gòu)造函數(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 木木頭 閱讀(414) 評論(0)  編輯 收藏 引用 所屬分類: C++特性

            導(dǎo)航

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

            統(tǒng)計

            常用鏈接

            留言簿(3)

            隨筆分類(29)

            搜索

            最新隨筆

            最新評論

            国产精品久久久久久| 一97日本道伊人久久综合影院| 婷婷久久综合九色综合98| 青青草原综合久久大伊人导航| 久久精品草草草| 亚洲一级Av无码毛片久久精品| 国産精品久久久久久久| 久久久国产亚洲精品| 热久久最新网站获取| 思思久久精品在热线热| 成人综合伊人五月婷久久| 国产精品美女久久久免费| 色欲久久久天天天综合网| 一本大道久久东京热无码AV| 久久精品久久久久观看99水蜜桃 | 久久精品成人免费国产片小草| 三级三级久久三级久久| 久久综合噜噜激激的五月天| 久久久青草久久久青草| 国产成人精品综合久久久| 久久久久久亚洲精品无码| 久久国产劲爆AV内射—百度| 99久久精品国产综合一区| 久久精品国产色蜜蜜麻豆| 青青草国产精品久久久久| 久久精品国产亚洲αv忘忧草| 精品久久久久久久久中文字幕| 亚洲国产成人乱码精品女人久久久不卡 | 思思久久好好热精品国产| 中文字幕久久欲求不满| 久久综合亚洲欧美成人| 国产精品中文久久久久久久| 亚洲国产精久久久久久久| 久久精品国产99久久久| 国产亚洲精品久久久久秋霞| 狠狠色丁香婷婷综合久久来| 国产偷久久久精品专区| 最新久久免费视频| 亚洲乱码日产精品a级毛片久久 | 久久亚洲私人国产精品vA| 亚洲综合久久夜AV |