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

            Benjamin

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            如何實(shí)現(xiàn)虛構(gòu)造

            如果想在構(gòu)造函數(shù)中調(diào)用虛函數(shù),必須來模擬虛構(gòu)造函數(shù)。注意是虛擬并不是真正的虛構(gòu)造函數(shù)
            #include <iostream>
            #include <string>
            #include <stdexcept>
            #include <cstddef>
            #include <vector>

            #include "../purge.h"
            using namespace std;

            class Shape{
               Shape* e;
               //Prevent copy-construcetion & operator=
               Shape(Shape&);
               Shape operator=(Shape&);
            protected:
               Shape(){s=0;}
            public:
               virtual void draw() {s->draw();}
               virtual void erase() {s->erase();}
               virtual void test() {s->test();}
               virtual ~Shape(){
                  cout<<"~Shape"<<endl;
                  if(s){
                      cout<<"Making virtual call:";
                     s->erase();//Virtual call
                  }
                  cout << "delete s" ;
                  delete s;//The polymorphic deletion
                  //(delete 0 is legal: it proeuces a no-op)
                  }

                  class BadShapeCreation:public logic_error{
                  public:
                    BadShapeCration(srtring type):logic_error("Cannot create type"+"type"){}
                  };
                  Shape(sting type) throw(BadSharCreation);
            };
                     

            class Circle:public Shape{
               Circle(Circle&);
               Circle operator-(Circle&);
               Cirlcle() {}//private constructor
               friend class Shape;
            public:
               void draw(){cout << "Circle::draw"<<endl;}
               void erase(){cout <<"Circle::erase"<<endl;}
               void test(){draw();}
               ~Circle(){cout<<"Circle::~Circle"<<endl;}
            };

            class Square:public Shape{
               Square(Square&);
               Square operator-(Square&);
               Square() {}//private constructor
               friend class Shape;
            public:
               void draw(){cout << "Square::draw"<<endl;}
               void erase(){cout <<:Square::erase"<<endl;}
               void test(){draw();}
               ~Square(){cout<<"Square:~Circle"<<endl;}
            };

            Shape::Shape(stgring type) throw(Shape::BadShapeCreation){
               if(type=="Circle")
                  s = new Circle;
               else if(type =="Square")
                  s = new Square;
               else throw BadShapeCreation(type);
               draw();//Virtual call in the constructor
            }

            char *sl[] = {"Circle","Square","Square","Circle","Circle","Circle","Square"};

            int main(){
               vector<Shape*> shapes;
               cout <<"Virtual constructor calls"<<endl;
               try{
                     for(size_t i = 0;i<sizeof (sl)/sizeof (sl[0]); i++)
                     {
                           shapes.push_back(new Shapes(sl[i]]));
                            }catch(Shape::BadShapeCreation e){
                                       cout<<e.what()<<endl;
                                       purge(shapes);
                                       return EXIT_FAILURE;
                              }
                              for(size_t i = 0; i < shapes.size();i++){
                                    shapes[i]->draw();
                                    cout << "text" <<endl;
                                    shapes[i]->test();
                                    cout<<"end test"<<endl;
                                    shapes[i]->erase();
                              }
                        Shapes c("Circle");//create on the stack
                        cout<<"destructor calls:"<<endl;
                        purge(shapes);
            }
            它的思想是用基類的構(gòu)造來確定類型,并在堆上創(chuàng)建此類型的對象,然后通過一個(gè)基類的指針實(shí)現(xiàn)對派生類對象的操作。這其實(shí)是State狀態(tài)模型的變形,基類其實(shí)是個(gè)代理類。

            實(shí)現(xiàn)虛構(gòu)造的方法還有Scott Meyers在More Effective C++中提出的用虛拷貝構(gòu)造函數(shù)實(shí)現(xiàn)的方法,基類是個(gè)抽象基類,個(gè)人感覺上面的實(shí)現(xiàn)更好些。

            posted on 2009-04-19 22:18 Benjamin 閱讀(456) 評論(2)  編輯 收藏 引用 所屬分類: C/C++

            評論

            # re: 如何實(shí)現(xiàn)虛構(gòu)造  回復(fù)  更多評論   

            囧,這是一種奇怪的factory
            2009-04-20 14:15 | 陳梓瀚(vczh)

            # re: 如何實(shí)現(xiàn)虛構(gòu)造  回復(fù)  更多評論   

            @陳梓瀚(vczh)
            - -,居然在這發(fā)現(xiàn)你,,,
            2009-08-14 17:30 | MartySa
            热re99久久精品国99热| 久久精品无码av| 久久久久四虎国产精品| 久久久WWW成人免费毛片| 亚洲精品97久久中文字幕无码| 亚洲精品高清国产一线久久| 久久精品国产亚洲一区二区| 亚洲国产成人久久一区久久| 久久精品无码专区免费青青| 欧美精品一区二区久久| 99久久无色码中文字幕| 波多野结衣久久一区二区 | 久久嫩草影院免费看夜色| 久久久久久久精品妇女99| 久久久综合九色合综国产| 伊人久久精品影院| 99久久精品免费看国产免费| 天天躁日日躁狠狠久久| 伊人热热久久原色播放www| 国产成人久久精品二区三区| 热re99久久精品国99热| 亚洲一级Av无码毛片久久精品| 久久香蕉国产线看观看乱码| 久久影院综合精品| 99久久精品国产一区二区| 国内精品久久久久久久久电影网 | 久久777国产线看观看精品| 亚洲精品高清国产一线久久| 狠狠色丁香婷婷久久综合五月| 国产成人综合久久久久久| 国产精品美女久久久久久2018| 亚洲成色www久久网站夜月| 久久久久亚洲AV片无码下载蜜桃| 久久久久香蕉视频| 9999国产精品欧美久久久久久| 久久亚洲精品中文字幕| 久久无码人妻一区二区三区| 无码日韩人妻精品久久蜜桃 | 91久久精品91久久性色| 久久天天躁狠狠躁夜夜96流白浆 | 亚洲精品无码成人片久久|