• <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
            久久99久久成人免费播放| 日韩美女18网站久久精品| 无码八A片人妻少妇久久| 久久精品卫校国产小美女| 久久99久国产麻精品66| 国产成人精品白浆久久69| 久久精品国产亚洲5555| 看久久久久久a级毛片| 韩国三级中文字幕hd久久精品| 亚洲国产成人久久综合区| 99久久精品国产高清一区二区| 武侠古典久久婷婷狼人伊人| 99国产欧美精品久久久蜜芽| 久久人人爽人人爽AV片| 国产精品久久永久免费| 亚洲国产日韩综合久久精品| 99久久国产亚洲高清观看2024| 99久久精品国产一区二区 | 久久久久久久99精品免费观看| 久久综合亚洲色HEZYO国产| 久久99国产精品久久99果冻传媒| 精品久久久无码人妻中文字幕| 久久成人精品| 亚洲精品高清久久| 99热成人精品热久久669| 久久亚洲精品人成综合网| 91麻豆国产精品91久久久| 亚洲精品成人久久久| 久久综合日本熟妇| 久久久久香蕉视频| 国产精品美女久久久网AV| 99久久精品国产一区二区蜜芽 | 狠狠色婷婷久久综合频道日韩 | 亚洲va中文字幕无码久久| 中文字幕久久精品| 久久久久波多野结衣高潮| 久久婷婷人人澡人人爽人人爱| 久久久久亚洲国产| 亚洲日韩中文无码久久| 狠狠88综合久久久久综合网| 久久久亚洲裙底偷窥综合|