• <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
            <2011年3月>
            272812345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            享受編程

            常用鏈接

            留言簿(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
            久久91精品久久91综合| 久久久亚洲AV波多野结衣| 无码专区久久综合久中文字幕| 国产香蕉久久精品综合网| 久久无码AV中文出轨人妻| 久久久久无码精品国产不卡| 国产成年无码久久久久毛片 | 久久天天躁狠狠躁夜夜不卡| 色综合久久88色综合天天 | 97精品依人久久久大香线蕉97| 久久r热这里有精品视频| 亚洲国产精品嫩草影院久久| 99久久国产热无码精品免费| 国产精品99久久久久久www| 综合人妻久久一区二区精品| 伊人久久大香线蕉精品| 777午夜精品久久av蜜臀| 国产 亚洲 欧美 另类 久久| 亚洲综合伊人久久大杳蕉| 精品久久久久久久中文字幕| 亚洲国产另类久久久精品黑人| 久久综合九色欧美综合狠狠| 久久精品国产99久久丝袜| 成人国内精品久久久久一区| 香蕉久久久久久狠狠色| 久久国产视频网| 国产精品伦理久久久久久| 久久线看观看精品香蕉国产| 伊人久久精品无码二区麻豆| 久久伊人亚洲AV无码网站| 久久精品成人国产午夜| 精品无码久久久久久午夜| 欧美噜噜久久久XXX| 国产成人精品综合久久久| 久久精品卫校国产小美女| 亚洲国产成人久久精品99| 久久亚洲精品无码观看不卡| 久久99精品九九九久久婷婷| 国产成人久久777777| 久久久久久无码国产精品中文字幕| 久久精品嫩草影院|