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

            導航

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

            統計

            常用鏈接

            留言簿(3)

            隨筆分類(29)

            搜索

            最新隨筆

            最新評論

            亚洲人成无码久久电影网站| 国产精品一区二区久久精品涩爱| 久久久久久久久无码精品亚洲日韩| 亚洲精品乱码久久久久66| 久久大香香蕉国产| 性高朝久久久久久久久久| 久久久久免费看成人影片| 国产成人综合久久久久久| 久久频这里精品99香蕉久| 91精品久久久久久无码| 国产69精品久久久久9999APGF| 91久久福利国产成人精品| 国产精品亚洲综合久久| 国产AⅤ精品一区二区三区久久| 亚洲精品乱码久久久久久| 久久久WWW免费人成精品| 少妇人妻88久久中文字幕| 亚洲伊人久久成综合人影院| 狠狠色丁香久久综合五月| 久久精品国产99国产精品导航| 国产福利电影一区二区三区久久久久成人精品综合 | 国产成人精品久久亚洲高清不卡| 久久大香萑太香蕉av| 国产ww久久久久久久久久| 91精品国产色综合久久| 久久精品国产亚洲αv忘忧草| 久久久久久A亚洲欧洲AV冫| 亚洲国产精品久久久久婷婷软件| 国产综合久久久久久鬼色| 无码人妻精品一区二区三区久久 | 久久精品草草草| 国产精品一久久香蕉产线看| 国产精品9999久久久久| 久久国产精品无码一区二区三区| 亚洲第一极品精品无码久久| 99久久精品免费看国产一区二区三区 | 亚洲精品无码久久久| 怡红院日本一道日本久久 | 99久久超碰中文字幕伊人| 一本一道久久综合狠狠老| 国产成人精品综合久久久|