锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 榪欎釜渚嬪瓙鍙互鐪嬪仛鏄STL瀹瑰櫒璁捐浠庡ご鍒板熬鐨勪竴縐嶈璁猴紝鎴戞壙璁ゅ埌鏈鍚庣増鏈篃鍜?/span>STL涓殑瀹瑰櫒鏈夊緢澶х殑宸窛錛屼絾鏄垜浠彂鐜板湪榪欐牱鐨勮璁烘湁鍒╀簬鎴戜滑鏇磋繘涓姝ョ殑瀵?/span>STL鐨勮璁℃湁鏇存繁涓姝ョ殑浜嗚В銆傞鍏堬紝鍋囪鎴戜滑鎯蟲妸涓涓爢綾繪斁鍒頒竴涓暟緇勪腑錛屼緥濡傛槸涓浜涜〃紺轟笉鍚岀被鍨嬬殑浜ら氬伐鍏鳳紝褰撶劧涓嶅悓鐨勪氦閫氬伐鍏峰湪榪欓噷閮芥淳鐢熶箣涓涓熀紜綾伙細
絳夌瓑錛屾墍鏈夋淳鐢熶箣Vehicle綾婚兘蹇呴』瀹炵幇榪欎簺铏氭嫙鍑芥暟鍚﹀垯鏃犳硶瀹炰緥鍖栥?/span> 鐜板湪鎯沖榪欎簺浜ら氬伐鍏瘋繘琛岀鐞嗘妸瀹冩斁鍦ㄤ竴涓暟緇勪腑濡備笅錛?br> 褰撶劧榪欓噷鏈変竴涓弗閲嶇殑闂錛?span style="COLOR: red">error C2259: 'Vehicle' : cannot instantiate abstract class due to following members: Vehicle鏈夊涓殑綰櫄鍑芥暟榪欓噷鏄棤娉曞疄渚嬪寲鐨勶紝鍗充嬌鍙互榪欓噷鐨剎鍏跺疄鏄瑁佸壀浜嗗氨閫鍖栨垚Vehicle浜嗭紝騫朵笉鏄垜浠墍鎯崇殑璁╀粬瀹炵幇鍔ㄦ佽仈緙栥?span style="COLOR: red">榪欐槸鍒濆鐨勬椂鍊欑粡甯哥鍒扮殑涓浜涘皬闂錛屽彲浠ラ氳繃鎸囬拡瑙e喅銆?/span>
榪欐牱涓鏉ュ氨鍙互瑙e喅鍔ㄦ佽仈緙栫殑闂浜嗭紝鍚屾椂涔熷嚭鐜頒簡鏂扮殑闂錛岃繖閲岀殑x鏄眬閮ㄥ彉閲忓綋x鐨勭敓鍛藉懆鏈熷埌浜嗙殑鏃跺欙紝閭d箞Parking_lot[/* */]浠涔堜篃涓嶆槸浜嗐傛湁浜轟篃鎻愬嚭浜嗙敤鍔ㄦ佺敵璇風殑鏂規硶錛?/span>Parking_lot[num_vehiles++] = new Automoblie(x);浣嗘槸鐢寵鐨勮繖鍧楀唴瀛樿鐢辮皝鏉ラ噴鏀懼憿錛?span style="COLOR: red">涓涓ソ鐨勮В鍐沖姙娉曞氨鏄氳繃浠g悊璁╁畠浜嗙鐞嗗唴瀛樼殑閲婃斁闂銆?/span>榪欓噷鍦ㄦ瘡涓被涓兘鍔犲叆涓涓嫹璐濊嚜宸辯殑鍑芥暟銆傚涓嬶細 寤虹珛涓涓唬鐞嗙被濡備笅錛?/span> 榪欎釜綾諱腑鏈変竴涓寚鍚戜氦閫氬伐鍏風殑鎸囬拡T* vp錛屽氨鍙互鐢ㄦ潵瀛樻斁浠諱綍涓涓淳鐢熻嚦Vehicle鐨勭被錛屽彲浠ュ緩绔嬩竴涓暟緇勶細
鍦ㄥ線parking_lot鏀句笢瑗挎椂錛岀敤copy鍑芥暟榪涜鍔ㄦ佺殑鍒嗛厤涓涓璞★紝榪欎釜鍔ㄦ佸璞$殑閲婃斁鏄敱VehicleSurrogate鏉ョ鐞嗙殑錛?span style="COLOR: red">涓嶇敤鑷繁鎿嶅績錛屼笉浼氬紩璧峰唴瀛樼殑娉勯湶闂銆?/span>涓嬮潰鐪嬬湅VehicleSurrogate綾葷殑瀹炵幇錛?/span> 榪欓噷鎴戞兂璇寸殑鏄紝榪欎釜綾葷殑瀹炵幇鍜屼笓涓氱殑STL瀹瑰櫒鏄湁涓瀹氱殑宸窛鐨勶紝姣忔緇欏鍣ㄦ斁涓滆タ鐨勬椂鍊欓兘鏈夎皟鐢?/span>copy鏉?/span>new涓涓被錛岃繖鏍風殑鎿嶄綔鏈夌偣嫻垂鏃墮棿浜嗐?br>浠g爜
#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
{}


Automatebile x = /**//* */
Vehicle parking_lot[100];
Parking_lot[num_vehicles++]=x;
Vehicle* parking_lot[100];

Automatebile x = /**//**/
Parking_lot[num_vehiles++]=&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;
};
VehicleSurrogate<Vehicle> parking_lot[100];
parking_lot[num_vehicles++] = VehicleSurrogate<Vehicle>(RoadVehicle());

//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;
}
]]>
]]>