• <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久久国产综合精品| 国产精品视频久久久| 国产免费久久精品丫丫| 久久精品国产AV一区二区三区| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久成人精品| 国产91久久精品一区二区| 久久精品国产清自在天天线| 久久国产精品成人片免费| 久久久亚洲精品蜜桃臀| 狠狠色婷婷综合天天久久丁香| 日产久久强奸免费的看| 久久亚洲国产午夜精品理论片| 国产精品久久久久久久久软件| 国产一区二区精品久久| 亚洲国产精品无码久久一区二区| 久久强奷乱码老熟女网站| 亚洲综合精品香蕉久久网97| 亚洲精品乱码久久久久久| 久久一本综合| 亚洲国产二区三区久久| 99国产欧美久久久精品蜜芽 | 久久国产成人精品麻豆| 国产精品99久久久精品无码| 久久艹国产| 久久久精品国产Sm最大网站| 久久综合久久综合久久| 久久99免费视频| 久久99国产精品二区不卡| 国产精品岛国久久久久| 久久久久亚洲av无码专区喷水 | 久久久久99这里有精品10| 精品久久久久久久中文字幕| 曰曰摸天天摸人人看久久久| 久久九九青青国产精品| 久久精品国产亚洲欧美| 国产三级观看久久| 久久中文精品无码中文字幕| 亚洲欧洲精品成人久久奇米网|