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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            中文字幕乱码久久午夜| 久久久久久久久久久久中文字幕 | 无码人妻久久久一区二区三区| 国产成人精品综合久久久久| 午夜精品久久久久久影视777| 三上悠亚久久精品| 国产午夜精品久久久久九九电影| 免费精品国产日韩热久久| 国产一区二区精品久久| 亚洲国产成人精品女人久久久 | 精品人妻久久久久久888| 午夜精品久久久久久久无码| 国内精品人妻无码久久久影院导航 | 久久综合久久鬼色| 亚洲国产精品久久| 伊人久久大香线蕉亚洲| 国产精品成人精品久久久| 夜夜亚洲天天久久| 一本色道久久综合亚洲精品| 久久精品国产福利国产琪琪| 伊人久久综在合线亚洲2019| 中文字幕人妻色偷偷久久 | 久久久九九有精品国产| 国产激情久久久久久熟女老人| 久久久久综合网久久| 久久精品亚洲中文字幕无码麻豆| 久久久久人妻一区精品果冻| 久久九九全国免费| 国产一级持黄大片99久久| www.久久热| 日本免费久久久久久久网站| 久久青青草原亚洲av无码app| 国产精品久久久久久久久久影院 | 人妻中文久久久久| 国产综合免费精品久久久| 久久久精品免费国产四虎| 久久综合中文字幕| 久久www免费人成精品香蕉| 国产精品永久久久久久久久久| 久久91亚洲人成电影网站| 久久精品一区二区国产|