• <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
            伊人久久综合成人网| 97久久国产综合精品女不卡| 国产午夜精品理论片久久影视| 18禁黄久久久AAA片| 男女久久久国产一区二区三区| 久久久久久九九99精品| 办公室久久精品| 国产精品久久久久蜜芽| 久久亚洲综合色一区二区三区| 久久久久国产一区二区| 亚洲狠狠婷婷综合久久久久| 久久精品国产影库免费看| 日本久久中文字幕| 久久精品麻豆日日躁夜夜躁| 久久精品国产99国产精品| 久久水蜜桃亚洲av无码精品麻豆 | 久久久久久毛片免费看| 综合人妻久久一区二区精品| 99久久99久久精品国产片| 久久精品一本到99热免费| 99久久99久久精品国产片| 久久久精品人妻一区二区三区四 | 午夜精品久久久久久久| 久久久久国产亚洲AV麻豆| 狠狠狠色丁香婷婷综合久久五月| 久久人做人爽一区二区三区 | 精品久久久久久无码中文字幕| 亚洲日韩中文无码久久| 热久久最新网站获取| 国产精品综合久久第一页| 国产精品久久久久免费a∨| 久久亚洲欧美日本精品| 欧美色综合久久久久久| 亚洲国产精品无码久久久蜜芽| 99久久国产亚洲综合精品| 久久久久亚洲AV无码去区首| 国产毛片久久久久久国产毛片| 伊人久久综合热线大杳蕉下载| www.久久热.com| 国产免费久久精品99久久| 国产精品免费久久|