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

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            如何實現虛構造

            如果想在構造函數中調用虛函數,必須來模擬虛構造函數。注意是虛擬并不是真正的虛構造函數
            #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);
            }
            它的思想是用基類的構造來確定類型,并在堆上創建此類型的對象,然后通過一個基類的指針實現對派生類對象的操作。這其實是State狀態模型的變形,基類其實是個代理類。

            實現虛構造的方法還有Scott Meyers在More Effective C++中提出的用虛拷貝構造函數實現的方法,基類是個抽象基類,個人感覺上面的實現更好些。

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

            評論

            # re: 如何實現虛構造  回復  更多評論   

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

            # re: 如何實現虛構造  回復  更多評論   

            @陳梓瀚(vczh)
            - -,居然在這發現你,,,
            2009-08-14 17:30 | MartySa
            久久人人爽人人爽人人片AV不| 久久久久无码中| 一本久道久久综合狠狠躁AV| 一本大道久久东京热无码AV| 色婷婷久久综合中文久久蜜桃av| 亚洲欧美日韩久久精品第一区| 精品无码久久久久国产| 国产精品女同一区二区久久| 亚洲精品乱码久久久久久蜜桃| 久久午夜伦鲁片免费无码| 国产成人久久精品二区三区| 欧美伊人久久大香线蕉综合| 久久精品国产91久久综合麻豆自制| 久久久久香蕉视频| 久久久久亚洲av无码专区| 欧美日韩成人精品久久久免费看| 日日躁夜夜躁狠狠久久AV| 99久久国产免费福利| 人人狠狠综合久久88成人| 久久99精品久久久久久噜噜 | 国产成人综合久久综合| 久久狠狠高潮亚洲精品| 亚洲国产成人久久精品影视 | 国产午夜福利精品久久| 色婷婷综合久久久久中文| 国产精品久久久久久久久软件| 久久香蕉国产线看观看乱码| 天天躁日日躁狠狠久久 | 久久精品国产亚洲AV不卡| 久久A级毛片免费观看| 日韩AV无码久久一区二区| 久久久久亚洲AV无码观看| 一级a性色生活片久久无| 久久久久无码精品| 亚洲乱码日产精品a级毛片久久 | 少妇高潮惨叫久久久久久| 偷偷做久久久久网站| 久久青青国产| 亚洲日韩欧美一区久久久久我| 精品久久久久久无码中文野结衣 | 国内精品久久久久久久涩爱|