• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              62 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            ANSI/ISO C++ Professional Programmer's Handbook 》是這樣說的

            explicit Constructors
            A constructor that takes a single argument is, by default, an implicit conversion operator, which converts its argument to
            an object of its class (see also Chapter 3, "Operator Overloading"). Examine the following concrete example:
            class string
            {
            private:
            int size;
            int capacity;
            char *buff;
            public:
            string();
            string(int size); // constructor and implicit conversion operator
            string(const char *); // constructor and implicit conversion operator
            ~string();
            };
            Class string has three constructors: a default constructor, a constructor that takes int, and a constructor that
            constructs a string from const char *. The second constructor is used to create an empty string object with an
            initial preallocated buffer at the specified size. However, in the case of class string, the automatic conversion is
            dubious. Converting an int into a string object doesn't make sense, although this is exactly what this constructor does.

            Consider the following:
            int main()
            {
            string s = "hello"; //OK, convert a C-string into a string object
            int ns = 0;
            s = 1; // 1 oops, programmer intended to write ns = 1,
            }
            In the expression s= 1;, the programmer simply mistyped the name of the variable ns, typing s instead. Normally,
            the compiler detects the incompatible types and issues an error message. However, before ruling it out, the compiler first
            searches for a user-defined conversion that allows this expression; indeed, it finds the constructor that takes int.
            Consequently, the compiler interprets the expression s= 1; as if the programmer had written
            s = string(1);
            You might encounter a similar problem when calling a function that takes a string argument. The following example
            can either be a cryptic coding style or simply a programmer's typographical error. However, due to the implicit
            conversion constructor of class string, it will pass unnoticed:
            int f(string s);
            int main()
            {
            f(1); // without a an explicit constructor,
            //this call is expanded into: f ( string(1) );
            //was that intentional or merely a programmer's typo?
            }
            'In order to avoid such implicit conversions, a constructor that takes one argument needs to be declared explicit:
            class string
            {
            //...
            public:
            explicit string(int size); // block implicit conversion
            string(const char *); //implicit conversion
            ~string();
            };
            An explicit constructor does not behave as an implicit conversion operator, which enables the compiler to catch the
            typographical error this time:
            int main()
            {
            string s = "hello"; //OK, convert a C-string into a string object
            int ns = 0;
            s = 1; // compile time error ; this time the compiler catches the typo
            }
            Why aren't all constructors automatically declared explicit? Under some conditions, the automatic type conversion is
            useful and well behaved. A good example of this is the third constructor of string:
            string(const char *);

            The implicit type conversion of const char * to a string object enables its users to write the following:
            string s;
            s = "Hello";
            The compiler implicitly transforms this into
            string s;
            //pseudo C++ code:
            s = string ("Hello"); //create a temporary and assign it to s
            On the other hand, if you declare this constructor explicit, you have to use explicit type conversion:
            class string
            {
            //...
            public:
            explicit string(const char *);
            };
            int main()
            {
            string s;
            s = string("Hello"); //explicit conversion now required
            return 0;
            }
            Extensive amounts of legacy C++ code rely on the implicit conversion of constructors. The C++ Standardization
            committee was aware of that. In order to not make existing code break, the implicit conversion was retained. However, a
            new keyword, explicit, was introduced to the languageto enable the programmer to block the implicit conversion
            when it is undesirable. As a rule, a constructor that can be invoked with a single argument needs to be declared
            explicit. When the implicit type conversion is intentional and well behaved, the constructor can be used as an
            implicit conversion operator.
            posted on 2011-04-05 18:00 沛沛 閱讀(270) 評論(0)  編輯 收藏 引用 所屬分類: C++
            久久99热这里只有精品国产 | 久久91综合国产91久久精品| 人妻精品久久无码区| 国内精品久久久久影院一蜜桃 | 国产福利电影一区二区三区久久久久成人精品综合 | 少妇熟女久久综合网色欲| 伊人久久大香线蕉亚洲| 热久久国产精品| 久久久久青草线蕉综合超碰| 成人久久久观看免费毛片| 亚洲精品视频久久久| 97久久天天综合色天天综合色hd| 国产精品综合久久第一页| 国产成人精品三上悠亚久久| 国产99久久久久久免费看 | 亚洲精品乱码久久久久久| 中文字幕久久欲求不满| 精品综合久久久久久97| 99精品久久久久久久婷婷| 欧美亚洲色综久久精品国产| 人人狠狠综合久久亚洲高清| 久久久精品2019免费观看 | 久久久久亚洲AV无码永不| 久久精品国产亚洲Aⅴ香蕉| 久久人爽人人爽人人片AV| 一本色道久久综合狠狠躁篇| 91精品国产91久久| 免费观看成人久久网免费观看| 日本人妻丰满熟妇久久久久久| 日韩久久久久中文字幕人妻| 精品国产福利久久久| 国产麻豆精品久久一二三| 日韩人妻无码一区二区三区久久| 亚洲?V乱码久久精品蜜桃| 欧美无乱码久久久免费午夜一区二区三区中文字幕| 亚洲va久久久噜噜噜久久狠狠| 久久久久se色偷偷亚洲精品av| 一日本道伊人久久综合影| 午夜精品久久久内射近拍高清| 久久99久久成人免费播放| 四虎影视久久久免费|