• <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
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(lèi)(159)

            隨筆檔案(224)

            文章分類(lèi)(2)

            文章檔案(4)

            經(jīng)典c++博客

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            TitleType text here...
             

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

            #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類(lèi)都必須實(shí)現(xiàn)這些虛擬函數(shù)否則無(wú)法實(shí)例化。

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


             

            Automatebile x = /* */

            Vehicle parking_lot[
            100];

            Parking_lot[num_vehicles
            ++]=x;

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

            Vehicle* parking_lot[100];

            Automatebile x 
            = /**/

            Parking_lot[num_vehiles
            ++]=&x;

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

             

            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);

            }

             

            建立一個(gè)代理類(lèi)如下:

             

            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;

            }
            ;

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

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

            在往parking_lot放東西時(shí),用copy函數(shù)進(jìn)行動(dòng)態(tài)的分配一個(gè)對(duì)象,這個(gè)動(dòng)態(tài)對(duì)象的釋放是由VehicleSurrogate來(lái)管理的,不用自己操心,不會(huì)引起內(nèi)存的泄露問(wèn)題。下面看看VehicleSurrogate類(lèi)的實(shí)現(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;
            }

            這里我想說(shuō)的是,這個(gè)類(lèi)的實(shí)現(xiàn)和專(zhuān)業(yè)的STL容器是有一定的差距的,每次給容器放東西的時(shí)候都有調(diào)用copy來(lái)new一個(gè)類(lèi),這樣的操作有點(diǎn)浪費(fèi)時(shí)間了。
            代碼

            posted on 2009-03-08 16:37 漂漂 閱讀(308) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): STL
            久久精品国产2020| 久久综合成人网| 国产91色综合久久免费分享| 无码人妻少妇久久中文字幕蜜桃 | 欧美一区二区三区久久综合| 日韩精品久久无码中文字幕| 国产欧美久久一区二区| 少妇被又大又粗又爽毛片久久黑人| 无码八A片人妻少妇久久| 久久夜色精品国产亚洲| 天天躁日日躁狠狠久久| 久久久黄片| 久久精品aⅴ无码中文字字幕不卡| 久久99精品国产麻豆婷婷| 久久精品国产99久久久 | 亚洲人成无码网站久久99热国产| 欧美黑人又粗又大久久久| 久久精品国产精品亜洲毛片| 国产欧美一区二区久久| 99re这里只有精品热久久| 亚洲国产另类久久久精品小说| 国产精品热久久无码av| 97久久综合精品久久久综合| 亚洲中文字幕久久精品无码APP | 精品久久人人爽天天玩人人妻| 国产午夜免费高清久久影院| 久久综合久久自在自线精品自| 少妇人妻综合久久中文字幕| 欧美久久一级内射wwwwww.| 国产精品欧美亚洲韩国日本久久 | 狠狠色丁香婷婷久久综合| 天天综合久久一二三区| 久久精品中文字幕第23页| 久久se这里只有精品| 国产成人综合久久久久久| 亚洲国产精品一区二区久久| 久久精品免费观看| 国内精品久久久久久久久| 99久久人人爽亚洲精品美女| 久久精品国产99久久丝袜| 久久久久久久综合狠狠综合|