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

            導航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿(3)

            隨筆分類(29)

            搜索

            最新隨筆

            最新評論

            久久精品国产欧美日韩| 亚洲国产精品久久久久婷婷老年| 久久毛片免费看一区二区三区| 欧美麻豆久久久久久中文| 欧美精品国产综合久久| 国产精品美女久久久久| 色播久久人人爽人人爽人人片aV| 久久久久亚洲av成人网人人软件 | 日韩AV无码久久一区二区| 国产精品久久久天天影视| 浪潮AV色综合久久天堂| 97久久精品午夜一区二区| 青草影院天堂男人久久| 精品久久久久久久国产潘金莲| 久久AV无码精品人妻糸列| 国产国产成人久久精品| 一本色道久久HEZYO无码| 精品国产综合区久久久久久| 伊人久久大香线蕉无码麻豆| 久久国产精品成人免费| 久久午夜羞羞影院免费观看| 欧美午夜A∨大片久久 | 亚洲综合久久夜AV | 久久九九青青国产精品| 久久丫精品国产亚洲av不卡 | 色天使久久综合网天天 | 久久91精品久久91综合| 国内精品久久久久久不卡影院| 人妻少妇久久中文字幕一区二区| 久久久久国产日韩精品网站| 91精品国产高清91久久久久久| 亚洲精品美女久久久久99| 久久精品一区二区三区AV| 日韩美女18网站久久精品| 亚洲国产小视频精品久久久三级 | 四虎国产精品免费久久久| a高清免费毛片久久| 久久综合狠狠色综合伊人| 日本福利片国产午夜久久| 99久久免费只有精品国产| 久久久久国产一区二区|