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

            Where there is a dream ,there is hope

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            (轉自:http://grantren.javaeye.com/blog/43289)

            一 拷貝構造函數是C++最基礎的概念之一,大家自認為對拷貝構造函數了解么?請大家先回答一下三個問題:

            1. 以下函數哪個是拷貝構造函數,為什么?

            1. X::X(const X&);   
            2. X::X(X);   
            3. X::X(X&, int a=1);   
            4. X::X(X&, int a=1, b=2);  

             2. 一個類中可以存在多于一個的拷貝構造函數嗎?

            3. 寫出以下程序段的輸出結果, 并說明為什么? 如果你都能回答無誤的話,那么你已經對拷貝構造函數有了相當的了解。

            1. #include    
            2. #include    
            3.   
            4. struct X {   
            5.   template<typename T>   
            6.   X( T& ) { std::cout << "This is ctor." << std::endl; }   
            7.   
            8.   template<typename T>   
            9.     X& operator=( T& ) { std::cout << "This is ctor." << std::endl; }   
            10. };   
            11.   
            12. void main() {   
            13.   X a(5);   
            14.   X b(10.5);   
            15.   X c = a;   
            16.   c = b;   
            17. }  

             

            解答如下:

            1. 對于一個類X,如果一個構造函數的第一個參數是下列之一:
            a) X&
            b) const X&
            c) volatile X&
            d) const volatile X&
            且沒有其他參數或其他參數都有默認值,那么這個函數是拷貝構造函數. 

            1. X::X(const X&);  //是拷貝構造函數   
            2. X::X(X&, int=1); //是拷貝構造函數  

             2.類中可以存在超過一個拷貝構造函數, 

            1. class X {      
            2. public:      
            3.   X(const X&);      
            4.   X(X&);            // OK   
            5. };  

            注意,如果一個類中只存在一個參數為X&的拷貝構造函數,那么就不能使用const X或volatile X的對象實行拷貝初始化.

            1. class X {   
            2. public:   
            3.   X();   
            4.   X(X&);   
            5. };   
            6.     
            7. const X cx;   
            8. X x = cx;    // error   

            如果一個類中沒有定義拷貝構造函數,那么編譯器會自動產生一個默認的拷貝構造函數.
            這個默認的參數可能為X::X(const X&)或X::X(X&),由編譯器根據上下文決定選擇哪一個.

            默認拷貝構造函數的行為如下:
             默認的拷貝構造函數執行的順序與其他用戶定義的構造函數相同,執行先父類后子類的構造.
             拷貝構造函數對類中每一個數據成員執行成員拷貝(memberwise Copy)的動作.
             a)如果數據成員為某一個類的實例,那么調用此類的拷貝構造函數.
             b)如果數據成員是一個數組,對數組的每一個執行按位拷貝.
             c)如果數據成員是一個數量,如int,double,那么調用系統內建的賦值運算符對其進行賦值.

             

            3.  拷貝構造函數不能由成員函數模版生成. 

            1. struct X {   
            2.     template<typename T>   
            3.     X( const T& );    // NOT copy ctor, T can't be X   
            4.   
            5.     template<typename T>   
            6.     operator=( const T& );  // NOT copy ass't, T can't be X   
            7. };   
            8.   

            原因很簡單, 成員函數模版并不改變語言的規則,而語言的規則說,如果程序需要一個拷貝構造函數而你沒有聲明它,那么編譯器會為你自動生成一個. 所以成員函數模版并不會阻止編譯器生成拷貝構造函數, 賦值運算符重載也遵循同樣的規則.(參見Effective C++ 3edition, Item45)


            二 針對上面作者的討論,理解更深了,但是下面我還是會給出一個一般的標準的實現和注意事項:
            #include "stdafx.h"
            #include 
            "stdio.h"
            #include 
            <iostream>
            #include 
            <string>


            struct Test1 
            {
                Test1() 
            { }
                Test1(
            int i) { id = i; }
                Test1(
            const Test1& test)
                
            {
                    id 
            = test.id;
                }

                Test1
            & operator = (const Test1& test)
                
            {
                    
            if(this == &test)
                        
            return *this;
                    id 
            = test.id;
                    
            return *this;
                }

                
            int id;
            }
            ;

            class Test2
            {
            public:
                Test2()
            { m_pChar = NULL;}
                Test2(
            char *pChar) { m_pChar = pChar;}
                Test2(
            int num) 
                

                    m_pChar 
            = new char[num];
                    
            for(int i = 0; i< num; ++i)
                        m_pChar[i] 
            = 'a';
                    m_pChar[num
            -1= '\0';
                }

                Test2(
            const Test2& test)
                
            {
                    
            char *pCharT = m_pChar;

                    m_pChar 
            = new char[strlen(test.m_pChar)];
                    strcpy(m_pChar, test.m_pChar);

                    
            if(!pCharT)
                        delete []pCharT;
                }

                Test2
            & operator = (const Test2& test)
                
            {
                    
            if(this == &test)
                        
            return *this;

                    
            char *pCharT = m_pChar;
                    m_pChar 
            = new char[strlen(test.m_pChar)];
                    strcpy(m_pChar, test.m_pChar);

                    
            if(!pCharT)
                        delete []pCharT;

                    
            return *this;
                }

            private:
                
            char *m_pChar;
            }
            ;

            int main(int argc, char* argv[])
            {
                
            const Test1 ts(1); // Test1()
                const Test1* p_ts = &ts;
                
            const Test1 ts2(ts); //Test(const Test1& test)
                const Test1 ts3 = ts; //Test(const Test1& test)
                Test1 ts4; ts4 = ts;  //Test1& operator = (const Test1& test)

                Test2 t(
            5);
                Test2 t2(t);
                Test2 t3 
            = t2;
                Test2 t4; t4 
            = t;
                
            return 0;
            }

            posted on 2011-02-14 17:38 IT菜鳥 閱讀(313) 評論(0)  編輯 收藏 引用
            国产精品久久国产精品99盘| 亚洲а∨天堂久久精品9966| 日韩欧美亚洲综合久久| 99久久国产免费福利| AV无码久久久久不卡蜜桃| 人人狠狠综合久久88成人| 亚洲精品乱码久久久久久蜜桃不卡 | 精品人妻伦一二三区久久| 狠狠色婷婷综合天天久久丁香 | 久久棈精品久久久久久噜噜| 蜜桃麻豆WWW久久囤产精品| 国产一区二区久久久| 一本色道久久综合亚洲精品| 国产亚洲精久久久久久无码77777| 2021最新久久久视精品爱| 精品久久久无码人妻中文字幕| 一本色综合网久久| 潮喷大喷水系列无码久久精品| 精品久久久久久久| 久久中文精品无码中文字幕 | 亚洲精品无码久久久久去q | 国产精品99久久不卡| 久久精品亚洲福利| 久久狠狠爱亚洲综合影院| 久久本道伊人久久| 久久综合久久鬼色| 久久久女人与动物群交毛片| 国产精品伊人久久伊人电影| 国产精品99久久久久久宅男小说| 久久AV高清无码| 色欲综合久久躁天天躁| 久久精品www人人爽人人| 久久av高潮av无码av喷吹| 亚洲欧美日韩久久精品第一区| 99久久精品免费| 久久人人妻人人爽人人爽| 久久精品亚洲欧美日韩久久| 久久国产精品成人片免费| 无码乱码观看精品久久| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久综合狠狠综合久久97色|