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

            MyMSDN

            MyMSDN記錄開發(fā)新知道

            單參數(shù)構(gòu)造函數(shù)的問題,single-argument construct

            關(guān)于單一參數(shù)構(gòu)造函數(shù)的問題,主要是因?yàn)閱我粎?shù)被編譯器用作隱式類型轉(zhuǎn)換,從而導(dǎo)致一些不可預(yù)期的事件的發(fā)生,請(qǐng)參見代碼詳細(xì)注釋:

            /*
             * single_argument_ctor.cpp
             *
             *  Created on: 2010-3-29
             *      Author: Volnet
             *      Compiler: GNU C++(version 3.4.5)
             *                MSVC CL(version 15.00.30729.01)
             */
            
            #include <stdlib.h>
            #include <iostream>
            #include <sstream>
            
            // single-argument class
            class SingleArgumentClass {
            private:
                int _inner;
            public:
                SingleArgumentClass()
                    :_inner(-1)
                {
            
                }
                SingleArgumentClass(int actual)
                    :_inner(actual)
                {
            
                }
                bool operator==(const SingleArgumentClass& rhs);
                std::string str(){
                    // we'd better to use boost::lexical_cast to cast.
                    // #region cast
                    std::stringstream strStream;
                    strStream << _inner;
                    std::string str;
                    strStream >> str;
                    // #endregion
                    return str;
                }
            };
            bool
            SingleArgumentClass::operator ==(const SingleArgumentClass& rhs){
                if(_inner == rhs._inner)
                    return true;
                return false;
            }
            
            // single-argument class fixed bug by explicit keyword.
            class SingleArgumentClassFixedBugByExplicitKeyword {
            private:
                int _inner;
            public:
                SingleArgumentClassFixedBugByExplicitKeyword()
                    :_inner(-1)
                {
            
                }
                explicit SingleArgumentClassFixedBugByExplicitKeyword(int actual)
                    :_inner(actual)
                {
            
                }
                bool operator==(const SingleArgumentClassFixedBugByExplicitKeyword& rhs);
                std::string str(){
                    // we'd better to use boost::lexical_cast to cast.
                    // #region cast
                    std::stringstream strStream;
                    strStream << _inner;
                    std::string str;
                    strStream >> str;
                    // #endregion
                    return str;
                }
            };
            bool
            SingleArgumentClassFixedBugByExplicitKeyword::operator ==(const SingleArgumentClassFixedBugByExplicitKeyword& rhs){
                if(_inner == rhs._inner)
                    return true;
                return false;
            }
            
            // single-argument class fixed bug by helper class.
            class ActualType {
            public:
                ActualType(int value):_value(value){};
                int get_value(){ return _value; }
            private:
                int _value;
            };
            class SingleArgumentClassFixedBugByHelperClass {
            private:
                int _inner;
            public:
                SingleArgumentClassFixedBugByHelperClass()
                    :_inner(-1)
                {
            
                }
                SingleArgumentClassFixedBugByHelperClass(ActualType actual)
                    :_inner(actual.get_value())
                {
            
                }
                bool operator==(const SingleArgumentClassFixedBugByHelperClass& rhs);
                std::string str(){
                    // we'd better to use boost::lexical_cast to cast.
                    // #region cast
                    std::stringstream strStream;
                    strStream << _inner;
                    std::string str;
                    strStream >> str;
                    // #endregion
                    return str;
                }
            };
            bool
            SingleArgumentClassFixedBugByHelperClass::operator ==(const SingleArgumentClassFixedBugByHelperClass& rhs){
                if(_inner == rhs._inner)
                    return true;
                return false;
            }
            
            
            void Assert(bool status,
                    std::string strTrue = std::string("assert result is true;"),
                    std::string strFalse = std::string("assert result is false;"));
            int main(void){
                SingleArgumentClass obj(3);
                std::cout << obj.str() << std::endl;
            
                // our purpose.
                SingleArgumentClass obj1(1);
                SingleArgumentClass obj2(1);
                Assert(obj1 == obj2, "obj1 == obj2", "obj1 != obj2");
            
                int i = 3;
                // warning!!!
                // obj is a SingleArgumentClass object.
                // i is a integer.
                // operator== only define the equal between two SingleArgumentClass object.
                // In fact:
                //    obj == i:
                //        1.compiler found the operator== require two SingleArgumentClass object.
                //        2.compiler try to find a cast method for casting int to SingleArgumentClass.
                //        3.compiler found it can use the SingleArguementClass.Ctor(int)
                //            to create a new SingleArgumentClass.
                //         4.compiler try to create a new SingleArgumentClass object from i.
                //        5.so it without any warning and error, but it's logical not we need.
                Assert(obj == i, "obj == i //right?", "obj != i");
                // Assert(i == obj); // Compile ERROR: no match for 'operator==' in 'i == obj'
            
                // it's may encounter a compile-time error.
                // GNU G++: no match for 'operator==' in 'objFixed == i'    single_argument_ctor.cpp    single_argument_ctor/src    106    C/C++ Problem
                // MSVC: 錯(cuò)誤    1    error C2679: 二進(jìn)制“==”: 沒有找到接受“int”類型的右操作數(shù)的運(yùn)算符(或沒有可接受的轉(zhuǎn)換)    {projectpath}\single_argument_ctor\src\single_argument_ctor.cpp    107    single_argument_ctor
                SingleArgumentClassFixedBugByExplicitKeyword objFixed(3);
                // Assert(objFixed == i, "objFixed == i", "objFixed != i");
            
                SingleArgumentClassFixedBugByHelperClass objFixedByHelper1(3);
                SingleArgumentClassFixedBugByHelperClass objFixedByHelper2(3);
                // it's may encounter a compile-time error.
                // GNU G++: no match for 'operator==' in 'objFixedAuto1 == i'    single_argument_ctor.cpp    single_argument_ctor/src    158    C/C++ Problem
                // MSVC: 錯(cuò)誤    1    error C2679: 二進(jìn)制“==”: 沒有找到接受“int”類型的右操作數(shù)的運(yùn)算符(或沒有可接受的轉(zhuǎn)換)    {projectpath}\single_argument_ctor\src\single_argument_ctor.cpp    163    single_argument_ctor
                // Assert(objFixedByHelper1 == i);
            }
            void Assert(bool status,
                    std::string strTrue, std::string strFalse)
            {
                std::cout << (status  strTrue : strFalse) << std::endl;
            }
            

            解決方法:

            1、explicit關(guān)鍵字,在單參數(shù)構(gòu)造函數(shù)前使用explicit參數(shù),可以避免單參數(shù)構(gòu)造函數(shù)被用于隱式轉(zhuǎn)換。

            2、利用中間類的方式,詳見代碼“SingleArgumentClassFixedBugByHelperClass相關(guān)部分”。如果編譯器不支持解決方法1,則建議使用此方法。上面代碼所提及的兩款主流編譯器均支持explicit關(guān)鍵字。

            posted on 2010-03-29 15:37 volnet 閱讀(1893) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C/C++

            特殊功能
             
            99久久做夜夜爱天天做精品| 精品人妻伦九区久久AAA片69| 国产三级精品久久| 久久久久亚洲AV无码去区首| 99久久香蕉国产线看观香| 国内精品久久久久伊人av| 999久久久免费国产精品播放| 久久这里的只有是精品23| 97热久久免费频精品99| 麻豆精品久久久久久久99蜜桃 | 91精品国产91久久久久久青草| 久久97久久97精品免视看| 亚洲中文字幕无码久久2020| 精品久久人人妻人人做精品| 久久w5ww成w人免费| 伊人伊成久久人综合网777| 丁香五月综合久久激情| 99久久无码一区人妻a黑| 成人综合久久精品色婷婷| 免费一级做a爰片久久毛片潮| 99久久国产综合精品麻豆| 久久精品国产亚洲AV嫖农村妇女| 欧美日韩成人精品久久久免费看 | 久久人人爽人人爽人人片AV麻烦| 国产叼嘿久久精品久久| 久久中文字幕一区二区| 日韩精品无码久久久久久| 久久香蕉国产线看观看猫咪?v| 国产精品岛国久久久久| 一本色道久久综合狠狠躁| 欧美日韩精品久久免费| 久久久黄色大片| 欧美日韩精品久久免费| 久久无码AV中文出轨人妻| 99久久免费国产精品特黄| 国产精品久久婷婷六月丁香| 一本综合久久国产二区| 亚洲伊人久久大香线蕉综合图片| 亚洲精品乱码久久久久久按摩 | 亚洲国产精品综合久久网络| 四虎影视久久久免费|