• <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年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(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è)堆類放到一個(gè)數(shù)組中,例如是一些表示不同類型的交通工具,當(dāng)然不同的交通工具在這里都派生之一個(gè)基礎(chǔ)類:

            #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類都必須實(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è)類中都加入一個(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è)代理類如下:

             

            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è)類中有一個(gè)指向交通工具的指針T* vp,就可以用來(lái)存放任何一個(gè)派生至Vehicle的類,可以建立一個(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類的實(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è)類的實(shí)現(xiàn)和專業(yè)的STL容器是有一定的差距的,每次給容器放東西的時(shí)候都有調(diào)用copy來(lái)new一個(gè)類,這樣的操作有點(diǎn)浪費(fèi)時(shí)間了。
            代碼

            posted on 2009-03-08 16:37 漂漂 閱讀(303) 評(píng)論(0)  編輯 收藏 引用 所屬分類: STL
            中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久天天躁夜夜躁狠狠躁2022 | 久久青草国产精品一区| 亚洲国产综合久久天堂| 好属妞这里只有精品久久| 久久久久久亚洲精品影院| 色综合久久久久网| 无码人妻精品一区二区三区久久久| 91精品久久久久久无码| 国内精品久久久久影院优| 亚洲国产一成久久精品国产成人综合 | 国产精品久久久久久| 亚洲精品无码久久久久去q| 久久人人爽人人澡人人高潮AV| 久久精品国产精品青草app| 久久久久亚洲AV无码麻豆| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久久久波多野结衣高潮| 国产日韩久久久精品影院首页| 国产精品一区二区久久精品| 蜜臀久久99精品久久久久久小说 | 99久久精品免费看国产一区二区三区| 国内精品久久久久久久coent| 国产美女久久久| 国产精品久久久久久久| 久久精品中文无码资源站| 久久综合精品国产二区无码| 大香伊人久久精品一区二区| 欧美一级久久久久久久大片| 国产真实乱对白精彩久久| 99久久成人18免费网站| 热久久国产精品| 国产精品永久久久久久久久久| 97久久精品人人做人人爽| 亚洲午夜久久影院| 国产午夜电影久久| 国产日韩久久免费影院| 久久夜色撩人精品国产| 香蕉久久AⅤ一区二区三区| 亚洲人成电影网站久久| 99久久做夜夜爱天天做精品|