After reading chapter 5th of "C ++ meditation" "ruminations on C ++", I put the code here according to the idea of proxy, so that I can check it later.
/** <Br/> * @ file surrogate. CPP <br/> * @ brief <br/> * @ author liugang <br/> * @ version 1.0 <br/> * @ date 2009-11-29 <br/> */<br /># include <iostream> <br/> using namespace STD; <br/> //////////////////////////////////// //// // <br/> // base class <br/> class vehicle {<br/> public: <br/> virtual double weight () const = 0; <br/> virtual void start () = 0; <br/> Virtual Vehicle * Copy () const = 0; <br/> virtual ~ Vehicle () {}< br/> }; <br/> //////////////////////////////////// /// // <br/> // surrogate class <br/> class vehiclesurrogate: public Vehicle {<br/> Public: <br/> vehiclesurrogate (); <br/> vehiclesurrogate (const Vehicle &); <br/> ~ Vehiclesurrogate (); <br/> vehiclesurrogate (const vehiclesurrogate &); <br/> vehiclesurrogate & operator = (const vehiclesurrogate &); <br/> // Implement Vehicle <br/> double weight () const; <br/> void start (); <br/> vehicle * Copy () const {return New vehiclesurrogate (* This) ;}< br/> PRIVATE: <br/> vehicle * VP; <br/>}; <br/> vehiclesurrogate :: vehiclesurrogate (): VP (0) <br/>{< br/>}< br/> vehiclesurrogate: vehiclesu Rrogate (const Vehicle & V): <br/> VP (V. Copy () <br/>{< br/>}< br/> vehiclesurrogate ::~ Vehiclesurrogate () <br/>{< br/> Delete VP; <br/>}< br/> vehiclesurrogate: vehiclesurrogate (<br/> const vehiclesurrogate & V ): <br/> VP (v. VP? V. VP-> copy (): 0) <br/>{< br/>}< br/> vehiclesurrogate & vehiclesurrogate: Operator = (const vehiclesurrogate & V) <br/>{< br/> If (this! = & V) {<br/> Delete VP; <br/> Vp = (V. VP? V. VP-> copy (): 0); <br/>}< br/> return * This; <br/>}< br/> double vehiclesurrogate: weight () const <br/> {<br/> If (Vp = 0) <br/> throw "Empty vehiclesurrogate. weight () "; <br/> return VP-> weight (); <br/>}< br/> void vehiclesurrogate: Start () <br/> {<br/> If (Vp = 0) <br/> throw "Empty vehiclesurrogate. start () "; <br/> VP-> Start (); <br/>}< br/> /////////////////////////////// /// // <br/> // Class automobile <br/> class automobile: public vehicle {<br/> Public: <br/> // automobile (){}; <br/> // automobile (double weight) {fweight = weight;} <br/> // automobile (double weight): fweight (weight) {}< br/> // automobile (double weight = 6.0): fweight (weight) {}< br/> //~ Automobile () {}// here It isn' t necessary, because there is no dynamic memory to free; <br/> // double weight () const {return fweight ;} <br/> double weight () const {return 6.0 ;}< br/> void start () {cout <"start... "<Endl ;}< br/> vehicle * Copy () const {return new automobile (* This) ;}< br/> PRIVATE: <br/> // double fweight; <br/> }; <br/> //////////////////////////////////// /// // <br/> int main (INT argc, char * argv []) <br/>{< br/> int num_vehicles = 0; <br/> vehiclesurrogate parking_lot [1000]; <br/> automobile X; <br/> parking_lot [num_vehicles ++] = x; <br/> // parking_lot [num_vehicles ++] = vehiclesurrogate (x); <br/> return 0; <br/>}< br/>/* VIM: Set Ts = 8 SW = 8 :*/