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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            国产成人综合久久综合| 久久天天躁狠狠躁夜夜2020一| 国产精品99久久久精品无码| 久久成人国产精品免费软件| 久久精品国产亚洲av影院| 91精品观看91久久久久久| 久久噜噜久久久精品66| 久久久久久久女国产乱让韩| 久久久久久亚洲AV无码专区| 精品国产青草久久久久福利 | 亚洲国产高清精品线久久 | 久久久久99精品成人片直播| 狠色狠色狠狠色综合久久| 国内精品久久久久久中文字幕| 2020久久精品亚洲热综合一本 | 久久WWW免费人成—看片| 色综合久久综合中文综合网| 久久国产影院| 好属妞这里只有精品久久| 模特私拍国产精品久久| 精品国产婷婷久久久| 99久久婷婷免费国产综合精品| 久久婷婷午色综合夜啪| 精品久久国产一区二区三区香蕉 | 久久亚洲欧洲国产综合| 99久久人妻无码精品系列| 狠狠色婷婷久久综合频道日韩| 精品久久久久久无码人妻热 | 7777精品久久久大香线蕉| 国产福利电影一区二区三区久久久久成人精品综合 | 亚洲午夜无码AV毛片久久| 久久香蕉超碰97国产精品| 国内精品久久久久影院亚洲| 九九久久精品国产| 一级做a爱片久久毛片| 久久99久久99精品免视看动漫| 精品无码人妻久久久久久| 青青热久久综合网伊人| 欧美亚洲另类久久综合| 很黄很污的网站久久mimi色| 精品久久人人妻人人做精品|