• <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>
            隨筆 - 224  文章 - 41  trackbacks - 0
            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

            經典c++博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            TitleType text here...
             

                    這個例子可以看做是對STL容器設計從頭到尾的一種討論,我承認到最后版本也和STL中的容器有很大的差距,但是我們發(fā)現(xiàn)在這樣的討論有利于我們更進一步的對STL的設計有更深一步的了解。首先,假設我們想把一個堆類放到一個數組中,例如是一些表示不同類型的交通工具,當然不同的交通工具在這里都派生之一個基礎類:

            #include <iostream>

            class Vehicle 

            {
            public:

                   
            virtual void start() =0;

                   
            virtual double weight() const = 0;

                   Vehicle();

                   
            virtual ~Vehicle();

            }
            ;

            Class RoadVehicle: 
            public Vehicle{}

            Class AutoVehicle: 
            public Vehicle{}

            等等,所有派生之Vehicle類都必須實現(xiàn)這些虛擬函數否則無法實例化。

            現(xiàn)在想對這些交通工具進行管理把它放在一個數組中如下:


             

            Automatebile x = /* */

            Vehicle parking_lot[
            100];

            Parking_lot[num_vehicles
            ++]=x;

                當然這里有一個嚴重的問題,error C2259: 'Vehicle' : cannot instantiate abstract class due to following members: Vehicle有多個的純虛函數這里是無法實例化的,即使可以這里的x其實是被裁剪了就退化成Vehicle了,并不是我們所想的讓他實現(xiàn)動態(tài)聯(lián)編。這是初學的時候經常碰到的一些小問題,可以通過指針解決。

            Vehicle* parking_lot[100];

            Automatebile x 
            = /**/

            Parking_lot[num_vehiles
            ++]=&x;

                    這樣一來就可以解決動態(tài)聯(lián)編的問題了,同時也出現(xiàn)了新的問題,這里的x是局部變量當x的生命周期到了的時候,那么Parking_lot[/* */]什么也不是了。有人也提出了用動態(tài)申請的方法,Parking_lot[num_vehiles++] = new Automoblie(x);但是申請的這塊內存該由誰來釋放呢!一個好的解決辦法就是通過代理讓它了管理內存的釋放問題。這里在每個類中都加入一個拷貝自己的函數。如下:

             

            class Vehicle 

            {
            public:

                   
            virtual Vehicle* copy() const =0;

                   
            virtual void start() =0;

                   
            virtual double weight() const = 0;

                   Vehicle();

                   
            virtual ~Vehicle();

            }
            ;

            Vehicle
            * RoadVehicle::copy() const

            {
                   
            return new RoadVehicle(*this);

            }

             

            建立一個代理類如下:

             

            template <class T>

            class VehicleSurrogate 

            {
            public:
                   
            //constructor

                   VehicleSurrogate(
            const VehicleSurrogate&);

                   VehicleSurrogate(
            const T&);

                   VehicleSurrogate();

                   
            virtual ~VehicleSurrogate();

                   
            //override the operator

                   VehicleSurrogate
            <T>& operator =(const VehicleSurrogate &v);

                   T
            * operator->();
            private:    
                   
            //the point of the member

                   T
            * vp;

            }
            ;

            這個類中有一個指向交通工具的指針T* vp,就可以用來存放任何一個派生至Vehicle的類,可以建立一個數組:

            VehicleSurrogate<Vehicle> parking_lot[100];
            parking_lot[num_vehicles
            ++= VehicleSurrogate<Vehicle>(RoadVehicle());

            在往parking_lot放東西時,用copy函數進行動態(tài)的分配一個對象,這個動態(tài)對象的釋放是由VehicleSurrogate來管理的,不用自己操心,不會引起內存的泄露問題。下面看看VehicleSurrogate類的實現(xiàn):

             

            //in order to achieve Array 

            template 
            <class T>

            VehicleSurrogate
            <T>::VehicleSurrogate():vp(0)

            {

            }

            //delete the point Prevent memory leak

            template 
            <class T>

            VehicleSurrogate
            <T>::~VehicleSurrogate()

            {

                   
            if (vp)

                          delete vp;

            }


            //new a class to the vp

            template 
            <class T>

            VehicleSurrogate
            <T>::VehicleSurrogate(const T& v):vp(v.copy())

            {     

            }


            template 
            <class T>

            VehicleSurrogate
            <T>::VehicleSurrogate(const VehicleSurrogate<T> & v):vp(v.vp? v.vp->copy():0)

            {

            }

            template 
            <class T>

            VehicleSurrogate
            <T>& VehicleSurrogate<T>::operator =(const VehicleSurrogate &v)

            {
                   
            if (this != &v)//we must pay attention to yourself

                   
            {

                          delete vp;

                          vp 
            = (v.vp?v.vp->copy():0);

                   }

                   
            return *this;
            }



            //this is the different to the book, in order to dislodge the function start and weight

            template 
            <class T>

            T
            * VehicleSurrogate<T>::operator->()

            {
                   
            return vp;
            }

            這里我想說的是,這個類的實現(xiàn)和專業(yè)的STL容器是有一定的差距的,每次給容器放東西的時候都有調用copynew一個類,這樣的操作有點浪費時間了。
            代碼

            posted on 2009-03-08 16:37 漂漂 閱讀(300) 評論(0)  編輯 收藏 引用 所屬分類: STL
            久久久久女人精品毛片| 久久久WWW成人| 亚洲成色www久久网站夜月| 久久精品人人槡人妻人人玩AV| 色偷偷偷久久伊人大杳蕉| 青青青伊人色综合久久| 久久夜色精品国产亚洲av| 色欲av伊人久久大香线蕉影院| 久久电影网2021| 区久久AAA片69亚洲| 日韩亚洲欧美久久久www综合网 | 久久久久久国产精品美女| 久久伊人影视| 四虎国产精品免费久久5151| 久久人人青草97香蕉| 狠狠色伊人久久精品综合网 | 久久亚洲高清综合| 日韩精品无码久久久久久| 久久乐国产精品亚洲综合| 久久精品国产亚洲AV大全| 青青草原综合久久大伊人| 久久久久久A亚洲欧洲AV冫| 精品久久久久久| 久久亚洲精品中文字幕| 亚洲精品NV久久久久久久久久| 国产成人精品久久综合| 亚洲乱亚洲乱淫久久| 丁香狠狠色婷婷久久综合| 日产精品久久久久久久性色| 久久人妻少妇嫩草AV蜜桃| 国产精品久久久香蕉| 亚洲午夜精品久久久久久app| 久久国产成人午夜aⅴ影院| 国产精品久久毛片完整版| 精品一区二区久久久久久久网站| 久久人人爽人人人人片av| 久久99久久99精品免视看动漫| 日韩十八禁一区二区久久| 亚洲国产成人久久精品99| 亚洲国产精品无码久久九九| 欧美国产精品久久高清|