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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久中文精品无码中文字幕| 国内精品久久久人妻中文字幕| 久久99热狠狠色精品一区| 2021国产成人精品久久| 热RE99久久精品国产66热| 精品伊人久久大线蕉色首页| 国产成人久久激情91| 欧美成a人片免费看久久| 久久精品国产亚洲AV嫖农村妇女 | 国产精品久久毛片完整版| 青青青伊人色综合久久| 久久久久久久97| 久久久青草久久久青草| 久久99热这里只有精品国产| 久久久久久久久久久免费精品| 久久综合给合久久狠狠狠97色| 久久精品亚洲精品国产欧美| 久久精品人人槡人妻人人玩AV| 性做久久久久久久久| 99久久精品国产一区二区蜜芽| 一本一本久久a久久综合精品蜜桃| 国产精品欧美亚洲韩国日本久久| 亚洲国产精品无码久久久蜜芽| 久久精品视频91| 国产精品久久毛片完整版| 色综合久久无码五十路人妻| 四虎亚洲国产成人久久精品| 久久国产精品无码网站| 日韩精品国产自在久久现线拍 | 久久婷婷五月综合国产尤物app| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 久久久久人妻一区二区三区vr| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 99久久精品这里只有精品| 69国产成人综合久久精品| 日韩人妻无码一区二区三区久久 | 久久久婷婷五月亚洲97号色| 久久久久久久精品妇女99| 久久久久久久亚洲精品| 久久久久成人精品无码| 日本国产精品久久|