• <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
            久久综合久久鬼色| 久久久久久国产精品免费免费| 伊人伊成久久人综合网777| 人妻精品久久久久中文字幕一冢本| 久久久无码精品亚洲日韩蜜臀浪潮| 色综合久久88色综合天天| 久久综合九色综合网站| 久久久久国产一级毛片高清板| 色综合久久夜色精品国产| 国产真实乱对白精彩久久| 性色欲网站人妻丰满中文久久不卡| www亚洲欲色成人久久精品| 97精品久久天干天天天按摩 | 久久精品国产91久久麻豆自制| 色青青草原桃花久久综合| 麻豆一区二区99久久久久| 草草久久久无码国产专区| 亚洲va国产va天堂va久久| 久久影视国产亚洲| 久久免费精品一区二区| 久久强奷乱码老熟女网站| 东方aⅴ免费观看久久av| 国内精品伊人久久久久妇| 麻豆精品久久精品色综合| 亚洲国产欧洲综合997久久| 国内精品久久久久影院网站| 久久99国产精品尤物| 久久精品国产半推半就| 无码人妻久久一区二区三区| 欧美久久久久久| 人妻无码精品久久亚瑟影视 | 久久国产香蕉一区精品| 国产成人精品久久二区二区| 久久国产劲爆AV内射—百度| 亚洲国产视频久久| 青青草原综合久久大伊人| 国产精品久久久久久久久软件| 香蕉久久影院| AV色综合久久天堂AV色综合在| 午夜精品久久久久久中宇| 欧美黑人又粗又大久久久|