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

            linux&c++ R&D

            programing is a pleasure!

            How do you define a copy constructor or assignment operator for a class that contains a pointer to a (abstract) base class?

            If the class "owns" the object pointed to by the (abstract) base class pointer, use the Virtual Constructor Idiom in the (abstract) base class. As usual with this idiom, we declare a pure virtual clone() method in the base class:

             class Shape {
             public:
               
            ...
               virtual Shape* clone() const = 0;   
            // The Virtual (Copy) Constructor
               
            ...
             };

            Then we implement this clone() method in each derived class. Here is the code for derived class Circle:

             class Circle : public Shape {
             public:
               
            ...
               virtual Circle* clone() const;
               
            ...
             };
             
             Circle* Circle::clone() const
             {
               return new Circle(*this);
             }

            (Note: the return type in the derived class is intentionally different from the one in the base class.)

            Here is the code for derived class Square:

             class Square : public Shape {
             public:
               
            ...
               virtual Square* clone() const;
               
            ...
             };
             Square* Square::clone() const
             {
               return new Square(*this);
             }

            Now suppose that each Fred object "has-a" Shape object. Naturally the Fred object doesn't know whether the Shape is Circle or a Square or ... Fred's copy constructor and assignment operator will invoke Shape's clone() method to copy the object:

             class Fred {
             public:
               
            // p must be a pointer returned by new; it must not be NULL
               Fred(Shape* p)
                 : p_(p) { assert(p != NULL); }
              ~Fred()
                 { delete p_; }
               Fred(const Fred& f)
                 : p_(f.p_->clone()) { }
               Fred& operator= (const Fred& f)
                 {
                   if (this != &f) {              
            // Check for self-assignment
                     Shape* p2 = f.p_->clone();   
            // Create the new one FIRST...
                     delete p_;                   
            // ...THEN delete the old one
                     p_ = p2;
                   }
                   return *this;
                 }
               
            ...
             private:
               Shape* p_;
             };

            posted on 2007-05-15 13:48 丑石 閱讀(597) 評論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            欧美一区二区精品久久| 欧美国产精品久久高清| 99久久99久久| 久久人人爽人人精品视频| 久久频这里精品99香蕉久| …久久精品99久久香蕉国产| 日韩AV毛片精品久久久| 色诱久久久久综合网ywww| 91精品国产91热久久久久福利| 91麻豆国产精品91久久久| 久久精品国产亚洲一区二区| 久久亚洲国产精品成人AV秋霞| 国产精品久久影院| 伊人久久大香线蕉综合影院首页| 久久综合九色综合久99| 日韩久久久久久中文人妻| 伊人久久国产免费观看视频| 久久成人精品| 99久久精品免费国产大片| 色欲综合久久中文字幕网 | 99久久这里只精品国产免费| 狠狠色丁香婷婷综合久久来| 久久精品水蜜桃av综合天堂| 狠狠色婷婷久久综合频道日韩| 久久国产视屏| 久久免费视频6| 久久精品成人影院| 国产无套内射久久久国产| 91久久成人免费| 99久久精品九九亚洲精品| 老司机国内精品久久久久| 国产一级持黄大片99久久| 97久久久精品综合88久久| 久久99热只有频精品8| 91久久精品91久久性色| 久久久久高潮毛片免费全部播放| 99久久无色码中文字幕人妻| 精品国产乱码久久久久软件 | 久久久久免费精品国产| 久久久久久免费视频| 久久国产免费直播|