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

            Welcome to ErranLi's Blog!

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(12)

            搜索

            •  

            積分與排名

            • 積分 - 175914
            • 排名 - 151

            最新評論

            閱讀排行榜

                
            如果在派生類中要重載派生類的operator = ,那么在基類中一般不要重載operator = ,因為這樣會帶來很多麻煩.

            定義了兩個類:
             class CBase
            {  
            public:
               CBase()
               {
                    cout<<"CBase constructing ..."<<endl;
                }
               virtual ~CBase()
               {
                    cout<<"CBase destructing ..."<<endl;
                }
            public:
               CBase & operator=(const CBase & ent)
               {
                     cout<<"CBase operator = ... "<<endl;
                     return *this;
                }
            };

            class CDerive: public CBase
            {
            public:
                 CDerive()
                 {
                       cout<<"CDerive constructing 
                 }
                 ~CDerive()
                 {
                        cout<<"CDerive destructing ..."<<endl;
                  } 
            public:
                 CDerive & operator=(const CDerive & ent)
                {
                       cout<<"CDerive operator = ... "<<endl;
                       return *this;
                 }
            }

            定義如下操作:

            CBase * b1 = new CDerive();
            CBase * b2 = new CDerive();

            (*b1) = (*b2);

            可以看到其輸出為:
            CBase constructing ...
            CDerive constructing ...
            CBase constructing ...
            CDerive constructing ...
            CBase operator = ...
            CDerive destructing ...
            CBase destructing ...
            CDerive destructing ...
            CBase destructing

            而實際上,操作(*b1) = (*b2)是想把b1,b2實際所指類型的兩個對象之間進行賦值,但是它只是調用了基類的賦值操作,沒有執行其本身派生類的賦值操作。

            發現有兩種方法可以解決這個問題,如果能夠知道基類指針實際所指的是哪個派生類的的對象,直接類型轉換就可以了:

            (*((CDerive*)b1)) = (*((CDerive*)b2));

            這樣可以發現輸出了:CDerive operator = ... 。但是當要定義基類的指針的時候,往往隱藏了具體的派生類,即往往不知道指針到底實現的是哪個派生類,所以這種方法有很多局限性。

            這時候可以采用第二種方法,即在派生類中重載基類的賦值操作符,即首先把基類的operator=定義為virtual operator=,再在派生類中重載這個,如:

            CBase & operator=(const CBase & ent)
             {
                   const CDerive * p = reinterpret_cast<const CDerive *>(&ent);
                   return operator=((*p));
             }

            好的,當再執行 (*b1) = (*b2) 的時候就可以看到輸出:CDerive operator = ... 。


             在此發現在派生類中重載派生類的operator =的時候,如果在基類中也重載operator = ,這樣會帶來很多麻煩,可能會丟失很多也許不想丟失的數據。
             
            同樣,依次還存在問題:
            CBase * b1 = new CDerive();
            CDervie * d1 = new CDerive();

            (*b1) = (*d1);

            CBase * b2 = new CBase ();
            CDervie * d2 = new CDerive();
            (*b2) = (*d2);

            所以我個人覺得,如果一個基類有很多個派生類的,而派生類中又重載了派生類本身的賦值操作符的時候,則基類中最好不要去重載賦值操作符,不如直接用函數去賦值。正如所說那樣,運算符重載只是為了一種語法上的方便,是另外一種函數調用方式,如果不能帶來方便,就沒必要了,還不如直接用函數去代替。


            .............敬請指點..................
            posted on 2006-05-22 01:09 erran 閱讀(1651) 評論(2)  編輯 收藏 引用 所屬分類: C & C++

            Feedback

            # re: C++隨筆 關于virtual operator = 2006-06-02 08:26 LOGOS
            嗯,在有繼承的體系中,重載賦值操作符的確會發生很多災難性的事情。
            增加知識了。  回復  更多評論
              

            # re: C++隨筆 關于virtual operator = 2009-11-23 13:58 Noock Tian
            CBase & operator=(const CBase & ent)
            {
            const CDerive * p = reinterpret_cast<const CDerive *>(&ent);
            return operator=((*p));
            }
            這個實現是有問題的,返回值應該是
            CDerived& operator=(const CDerived& other)
            {
            CBase::operator=(other);
            // Copy new members in CDerived
            return *this;
            }
              回復  更多評論
              

            精品久久久久久久国产潘金莲 | 无码国产69精品久久久久网站| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久亚洲AV成人无码国产| AV狠狠色丁香婷婷综合久久| 久久精品国产99国产电影网 | 国产国产成人久久精品| 久久影视国产亚洲| 国产精品美女久久久| 欧美国产成人久久精品| 久久婷婷成人综合色综合| 国产精品久久久99| 久久精品亚洲AV久久久无码| 亚洲狠狠久久综合一区77777| 久久精品桃花综合| 国产精品成人99久久久久| 亚洲AV无码久久| 久久中文字幕无码专区| 精品久久久久久久| 亚洲国产精品无码久久一线| 久久99精品国产99久久6| 99久久99久久精品免费看蜜桃| 久久九九久精品国产| 久久综合狠狠色综合伊人| 亚洲中文字幕无码久久2017 | 中文字幕久久精品无码| 亚洲午夜无码久久久久小说| 国产激情久久久久影院小草| 久久夜色精品国产欧美乱| 伊人色综合久久天天人守人婷| 91亚洲国产成人久久精品| 久久久久久综合一区中文字幕| 国产毛片欧美毛片久久久| 亚洲国产日韩欧美综合久久| 久久久久国产精品三级网| 久久青草国产精品一区| 国产一区二区三区久久精品| 精品国产乱码久久久久久郑州公司| 久久精品国产亚洲AV忘忧草18| 2021国内久久精品| 久久精品国产欧美日韩99热|