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

            導航

            <2006年12月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統計

            常用鏈接

            留言簿(3)

            隨筆分類(29)

            搜索

            最新隨筆

            最新評論

            久久99热这里只有精品国产| 一本久久知道综合久久| 国产精品久久久久影视不卡| 91精品国产91久久久久久| 久久精品国产99久久香蕉| 狠狠色丁香婷婷久久综合 | 久久香蕉综合色一综合色88| 久久九九久精品国产免费直播| 国内精品人妻无码久久久影院导航| 久久精品www| 久久伊人五月丁香狠狠色| 欧美激情精品久久久久| 久久人人爽人人爽人人爽| 国产69精品久久久久99| 日韩人妻无码精品久久免费一| 精品国产婷婷久久久| 国产69精品久久久久777| 久久久久久国产a免费观看黄色大片| 久久狠狠色狠狠色综合| 一本一本久久A久久综合精品| 国内精品久久久久久久久电影网| 日韩精品久久久久久久电影蜜臀| 日本高清无卡码一区二区久久| 久久国产色AV免费观看| 亚洲欧美国产精品专区久久| 97超级碰碰碰碰久久久久| 69SEX久久精品国产麻豆| 久久午夜无码鲁丝片| 97精品依人久久久大香线蕉97 | 人人狠狠综合久久88成人| 久久99热这里只有精品66| 性做久久久久久久久老女人| 国产亚洲成人久久| 国产精品99久久久久久猫咪| 久久精品成人免费网站| 久久99久久99小草精品免视看| 久久综合狠狠综合久久综合88| 99久久夜色精品国产网站| 亚洲中文字幕无码久久精品1| 国产激情久久久久久熟女老人| 国产免费久久精品99re丫y|