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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            精品免费tv久久久久久久| 亚洲精品国产第一综合99久久| 久久亚洲精品成人无码网站| 亚洲色欲久久久久综合网| 中文字幕精品久久| 久久久久人妻一区精品色| 伊人久久综合热线大杳蕉下载| 无码8090精品久久一区| 新狼窝色AV性久久久久久| 久久亚洲精品视频| 亚洲va久久久久| 国产精品无码久久综合网| 热99RE久久精品这里都是精品免费 | 久久精品国产第一区二区| 国内精品久久久久影院老司| 久久国产精品99国产精| 久久亚洲国产精品五月天婷| 久久国产高清字幕中文| 国内精品人妻无码久久久影院导航| 91久久精品国产免费直播| 亚洲va久久久噜噜噜久久男同 | 久久―日本道色综合久久| 国内精品综合久久久40p| 国产三级精品久久| 久久成人精品视频| 亚洲AV日韩精品久久久久| 日韩一区二区三区视频久久| 丰满少妇人妻久久久久久4| 久久久久久亚洲AV无码专区| 久久久亚洲AV波多野结衣| 久久人与动人物a级毛片| 久久精品国产只有精品66| 国产69精品久久久久99| 久久综合九色综合久99| 国产一区二区精品久久| 99精品国产在热久久| 久久国产精品99精品国产| 国产精品毛片久久久久久久| 国产精品久久久久久福利漫画| 久久精品毛片免费观看| 国内精品久久国产大陆|