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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            无码伊人66久久大杳蕉网站谷歌| 看久久久久久a级毛片| 久久精品国产一区二区三区日韩| 2021精品国产综合久久| 熟妇人妻久久中文字幕| 久久精品午夜一区二区福利| 久久九九免费高清视频| 亚洲精品无码成人片久久| 乱亲女H秽乱长久久久| 国产精品99久久不卡| 久久精品国产亚洲AV高清热| 91精品免费久久久久久久久| 一本一本久久aa综合精品| 国产一区二区精品久久| 久久伊人中文无码| 久久久WWW成人免费毛片| 亚洲人成网亚洲欧洲无码久久 | 老男人久久青草av高清| 亚洲AV无码久久精品色欲| 91久久九九无码成人网站| 久久亚洲中文字幕精品一区| 精品无码人妻久久久久久| 久久婷婷综合中文字幕| 久久精品中文字幕无码绿巨人 | 国产精品内射久久久久欢欢| 久久99热这里只有精品国产| 久久免费视频1| 人人狠狠综合久久亚洲高清| 久久精品国产欧美日韩| 久久99国产综合精品女同| 国产欧美一区二区久久| 77777亚洲午夜久久多喷| 尹人香蕉久久99天天拍| 亚洲国产成人久久综合碰| 99久久精品国产一区二区三区 | 久久精品国产免费| 91精品国产乱码久久久久久| 国产情侣久久久久aⅴ免费| 久久精品国产亚洲AV香蕉| 久久人爽人人爽人人片AV| 欧美一区二区三区久久综|