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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久久波多野结衣高潮| 亚洲AV无码久久寂寞少妇| 国产亚洲精久久久久久无码| 久久婷婷五月综合色奶水99啪| 久久中文骚妇内射| 精品国产综合区久久久久久 | 久久精品麻豆日日躁夜夜躁| 精品久久久久久无码专区不卡 | 国产99久久九九精品无码| 日韩影院久久| 青青国产成人久久91网| 伊人久久大香线蕉综合5g| 国产精品视频久久久| 久久精品夜色噜噜亚洲A∨| AV无码久久久久不卡蜜桃| 最新久久免费视频| 久久久黄片| 国产综合精品久久亚洲| 精品久久久久久无码专区不卡| 免费精品国产日韩热久久| 精品欧美一区二区三区久久久| 国内精品人妻无码久久久影院| 午夜视频久久久久一区| 精品久久久久中文字| 99久久99这里只有免费费精品| 亚洲&#228;v永久无码精品天堂久久| 久久精品人人做人人爽97| 国产毛片欧美毛片久久久| 久久人人爽人人人人片av| 亚洲国产精品狼友中文久久久 | 亚洲欧美一区二区三区久久| 日本久久久久久中文字幕| 国产精品青草久久久久婷婷| 久久精品国产精品亚洲毛片| 久久九九久精品国产免费直播| 久久久久久久91精品免费观看| 精品久久久久久无码人妻蜜桃| 精品久久久久久国产免费了| 国产精品九九久久免费视频 | 久久久国产视频| 久久综合色老色|