• <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菜鳥 閱讀(315) 評論(0)  編輯 收藏 引用
            久久r热这里有精品视频| 夜夜亚洲天天久久| 中文字幕亚洲综合久久菠萝蜜| 91精品久久久久久无码| 久久精品免费网站网| 亚洲欧洲精品成人久久曰影片| 久久无码高潮喷水| 精品国产乱码久久久久久郑州公司 | 香港aa三级久久三级老师2021国产三级精品三级在 | 久久久99精品成人片中文字幕| 成人综合久久精品色婷婷| 久久99国产综合精品| 久久一本综合| 国产精品久久久久久久| 久久青青色综合| 国产精品一区二区久久精品无码| 久久午夜无码鲁丝片秋霞| 久久久久国产精品麻豆AR影院| 无码人妻久久久一区二区三区| 91精品国产91久久久久久蜜臀| 国内精品久久久久影院薰衣草 | 久久噜噜电影你懂的| 亚洲精品乱码久久久久久| 久久精品亚洲男人的天堂| 久久福利青草精品资源站免费| 亚洲午夜久久久久妓女影院 | 国产精品久久久久久久| 亚洲AV无码久久精品狠狠爱浪潮| 久久精品国产精品亚洲人人| 2021久久国自产拍精品| 亚洲va久久久噜噜噜久久狠狠| 亚洲精品成人久久久| 久久精品国产72国产精福利| 99久久综合狠狠综合久久| 青青青青久久精品国产h| 999久久久免费精品国产| 久久人人爽爽爽人久久久| 亚洲AV日韩AV天堂久久| 日韩精品久久无码中文字幕| 色诱久久久久综合网ywww| 久久久久亚洲精品天堂|