• <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 丑石 閱讀(590) 評論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲一本综合久久| 久久se精品一区精品二区| 国产精品九九久久免费视频 | 国产精品久久国产精品99盘 | 久久99精品久久久久久| 精品蜜臀久久久久99网站| 久久er热视频在这里精品| 久久亚洲国产精品123区| 久久精品国产AV一区二区三区| 无码国内精品久久人妻| 国产精品无码久久综合网| 精品国产乱码久久久久久人妻| 国产一区二区三区久久精品| 精品欧美一区二区三区久久久| 久久久久亚洲av成人网人人软件 | 日产精品久久久一区二区| 国产99久久久久久免费看 | 久久久久久综合一区中文字幕| 思思久久99热只有频精品66| 久久A级毛片免费观看| 青青青青久久精品国产h久久精品五福影院1421 | 一本久久a久久精品综合香蕉| 国产情侣久久久久aⅴ免费| 伊人久久大香线蕉综合5g| 18岁日韩内射颜射午夜久久成人| 亚洲中文字幕无码久久精品1| 久久久精品久久久久久 | 青青青伊人色综合久久| 亚洲AV无码久久精品蜜桃| 热99RE久久精品这里都是精品免费| 狠色狠色狠狠色综合久久| 久久综合亚洲欧美成人| 婷婷五月深深久久精品| 亚洲中文久久精品无码| 亚洲欧美日韩久久精品| 久久亚洲AV无码西西人体| 久久午夜综合久久| 久久综合九色综合网站| 国产精品99久久久精品无码| 99久久99久久精品国产片果冻 | 久久国产色AV免费看|