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

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            波多野结衣久久一区二区| 伊人久久成人成综合网222| 久久久无码一区二区三区| 亚洲国产精品高清久久久| 日本一区精品久久久久影院| 久久午夜福利电影| 久久久久久夜精品精品免费啦| 国产99久久精品一区二区| 四虎久久影院| aaa级精品久久久国产片| 亚洲欧美精品伊人久久| 久久久精品久久久久影院| 久久精品一区二区| 伊人久久大香线焦AV综合影院| AA级片免费看视频久久| 少妇精品久久久一区二区三区| 久久国产成人| 国产精品青草久久久久婷婷 | 欧洲人妻丰满av无码久久不卡| 99久久精品免费| 99久久无色码中文字幕| 熟妇人妻久久中文字幕| 久久成人国产精品免费软件| 国产午夜精品理论片久久| 久久99国产精一区二区三区| 久久中文骚妇内射| 久久受www免费人成_看片中文| 久久精品成人免费观看97| 天天久久狠狠色综合| 久久99国产精品久久99| 91精品国产高清久久久久久io| 亚洲国产另类久久久精品| 精品多毛少妇人妻AV免费久久| 久久一区二区三区99| 欧美性猛交xxxx免费看久久久| 91精品国产高清久久久久久91| 99久久免费国产特黄| 久久综合丝袜日本网| 99久久伊人精品综合观看| 国产午夜电影久久| 久久人人爽人人爽人人片AV东京热|