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

            搜索

            最新隨筆

            最新評論

            久久精品国产亚洲欧美| 久久婷婷五月综合97色| 久久亚洲国产欧洲精品一| 久久精品国产91久久麻豆自制| 久久精品无码午夜福利理论片| 一本大道加勒比久久综合| 久久久久亚洲AV无码专区网站 | 亚洲国产精品18久久久久久| 午夜精品久久久久久久| 伊人久久大香线蕉精品| 久久久久亚洲av成人网人人软件| 国产一区二区三区久久| 久久综合伊人77777| 国产亚洲精久久久久久无码| 国产欧美久久久精品影院| 日本一区精品久久久久影院| 中文成人久久久久影院免费观看| 久久久久久久99精品免费观看| 老司机午夜网站国内精品久久久久久久久 | 亚洲国产一成人久久精品| 久久精品国产亚洲AV不卡| 久久久精品国产sm调教网站| 久久人人爽人人爽AV片| 狠狠干狠狠久久| 少妇久久久久久被弄高潮| 一极黄色视频久久网站| 久久久精品久久久久久 | 亚洲精品乱码久久久久久蜜桃 | 久久久久国产日韩精品网站| 国产精品99久久精品| 久久免费小视频| 97久久超碰成人精品网站| 麻豆AV一区二区三区久久 | 国产女人aaa级久久久级| 国产三级观看久久| 久久91综合国产91久久精品| 久久亚洲春色中文字幕久久久| 久久久久久久波多野结衣高潮| 久久综合亚洲色一区二区三区| 亚洲欧洲久久久精品| 久久久久亚洲精品日久生情|