• <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久久香蕉国产| 日韩精品久久久久久| 国产精品乱码久久久久久软件| 亚洲国产成人久久一区久久| 久久无码高潮喷水| 欧美久久综合性欧美| 免费无码国产欧美久久18| 久久99国产综合精品免费| 久久影院亚洲一区| 久久精品中文字幕无码绿巨人| 久久夜色撩人精品国产小说| 国产∨亚洲V天堂无码久久久| 亚洲国产成人久久一区WWW| 精品久久久久久| 久久综合狠狠综合久久| 一本色道久久88综合日韩精品 | 久久国语露脸国产精品电影| 国产精品毛片久久久久久久 | 色8久久人人97超碰香蕉987| 香蕉久久永久视频| 狠狠人妻久久久久久综合蜜桃| 久久久久人妻精品一区| 区久久AAA片69亚洲| 日韩亚洲国产综合久久久| 青青青青久久精品国产h| 97久久精品无码一区二区天美| 伊人久久大香线蕉亚洲五月天| 久久精品极品盛宴观看| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久国产成人精品麻豆| 久久久久99精品成人片欧美 | 久久一区二区三区99| 精品久久人人爽天天玩人人妻| 久久99精品国产99久久6| 伊人久久大香线蕉精品| 免费精品99久久国产综合精品| 亚洲乱亚洲乱淫久久| 91精品久久久久久无码| 久久久久成人精品无码| 久久久无码精品亚洲日韩京东传媒| 久久国内免费视频|