• <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
            <2010年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

            經典c++博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            TitleType text here...
             

                    這個例子可以看做是對STL容器設計從頭到尾的一種討論,我承認到最后版本也和STL中的容器有很大的差距,但是我們發現在這樣的討論有利于我們更進一步的對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類都必須實現這些虛擬函數否則無法實例化。

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


             

            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了,并不是我們所想的讓他實現動態聯編。這是初學的時候經常碰到的一些小問題,可以通過指針解決。

            Vehicle* parking_lot[100];

            Automatebile x 
            = /**/

            Parking_lot[num_vehiles
            ++]=&x;

                    這樣一來就可以解決動態聯編的問題了,同時也出現了新的問題,這里的x是局部變量當x的生命周期到了的時候,那么Parking_lot[/* */]什么也不是了。有人也提出了用動態申請的方法,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函數進行動態的分配一個對象,這個動態對象的釋放是由VehicleSurrogate來管理的,不用自己操心,不會引起內存的泄露問題。下面看看VehicleSurrogate類的實現:

             

            //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;
            }

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

            posted on 2009-03-08 16:37 漂漂 閱讀(308) 評論(0)  編輯 收藏 引用 所屬分類: STL
            热综合一本伊人久久精品| 久久久久国产精品| 色偷偷偷久久伊人大杳蕉| 人妻少妇久久中文字幕一区二区| 亚洲AV无码久久精品成人| 久久综合久久综合久久| 色老头网站久久网| 免费观看久久精彩视频| 国内精品久久久久影院老司| 国产精品久久免费| 精品国产日韩久久亚洲| 91性高湖久久久久| 老色鬼久久亚洲AV综合| 人人狠狠综合88综合久久| 久久婷婷五月综合97色一本一本| 久久久久久一区国产精品| 久久精品国产福利国产秒| 亚洲香蕉网久久综合影视| 成人国内精品久久久久影院VR| 久久精品一本到99热免费| 欧美一级久久久久久久大片| 久久综合九色综合欧美狠狠| 国内精品久久人妻互换| 伊人久久综合无码成人网| 亚洲国产成人乱码精品女人久久久不卡 | 72种姿势欧美久久久久大黄蕉 | 久久91精品国产91久| 久久久久女教师免费一区| 久久久精品一区二区三区| 久久狠狠高潮亚洲精品| 亚洲国产精品无码久久一区二区| 久久久午夜精品| 久久久国产视频| 伊人久久大香线蕉成人| 伊人久久亚洲综合影院| 色悠久久久久久久综合网| 日本久久中文字幕| 久久亚洲国产最新网站| 中文字幕乱码久久午夜| 国产婷婷成人久久Av免费高清| 久久国产精品-久久精品|