C ++ Shen Si Lu Reading Notes (1) Agent

zhaozj2021-02-12  146

C Shen Si Lu Reading Notes (1) Agent

Each object of the proxy class represents another object, which can be an object located in a complete inheritance hierarchy. By using proxy classes in the container rather than object itself, control memory allocation can be resolved and put different types of objects in the same container in the same container.

******** Question ******** Abstract base class VEHICLE contains members double weight () constvoid start ()

The following 4 classes are derived from Vehicle, each class has its own unique member, such as Aircraft contains members void fly () RoadvehicleAutovehihiRcrafthelicopter

Design a C container, which has the ability to include the types of different types and related objects

Compress the entire derived level in an object type

******** Solution ******** @ 方案 1 1: Array storage base class object VEHICLE PARKING_LOT [1000]; @ 潜 问题: 1. Abstract base classes cannot be instantified 2. Even Not an abstract class, the storage-style object will also cause the object to be tailored @ 方案 方案 2: array storage base pointer VEHICLE * parking_lot [1000]; @ 潜 问题: 1. Store objects have their own survival @ 方案 3: array Storage The survival of the pointer object replica of the object copy is determined by the array, and the release array is also released in which all objects pointing are @ potential problems: The burden of moving memory management is only a static type, if you want to let Parking_lot [P] points to a newly built object, the actual type of this object, the same object to the object pointing to parking_lot [q], is the same as the object (without using the RTTI) @Pynium 4: array stores pointers pointing to the object copy, for derived Hierarchical Add Implicity Virtual Vehicle * Copy () const = 0; // Base class VEHICLE * AIRCRAFT :: Copy () const // Derived class {Return New Aircraft (* this);} must also add a false analysis for derived levels Structure @ Potential Problem: Explicitly Process Memory Assignment @ 方案 5: Agent class defines a behavior and a Vehicle object similar, and potentially representing all inherited from the Vehicle class object class us, can, vehiclesurrogate (); // can create VEHICLESURROGATE an array of objects VehicleSurrogate (const Vehicle &); // inherited by any object from Vehicle create proxy ~ Vehiclesurrogate (); VehicleSurrogate (const VehicleSurrogate &); VehicleSurrogate & operator = (const VehicleSurrogate &); private: Vehicle * vp; public: // from Vehicle Weight () Const; Void Start ();} VEHICLESURROGATE:: VEHICLESURROGATE: VEHICLESUROGATE (): VP (0) {} // can generate empty surrogate VehiclesURROGATE :: vehicles :: vehiclesurrog ATE (Const Vehicle & V): VP (v.copy ()) {} Vehicles :: VEHICLESURROGATE: VEHICLESURROGATE :: VEHICLESURROGATE :: VEHICLROGATE (Const Vehiclesurrogate & V): VP (v.vp? v.vp-> copy (): 0) {}Vehicles :: Operator = (const vehiclesurrogate & v) {if (this! = & V) {/ / Prevent the agent from copy to its own delete vp; vp = (v.vp? V.vp-> Copy (): 0);} Return * this;} // Operation from Vehicle Double Vehicles :: Weight () const {if (0 == VP) throw "EMPTY VEHICLESURROGATE.WEIGHT ()"; return vp-> weight ();

} void vehicles :: start () {if (0 == VP) throw "Empty Vehiclesurrogate.Start ()"; vp-> start ();} * empty agent: behavior is similar to zero pointer, you can create, destroy and copy Such a proxy, but other operations are considered an error using the proxy class VEHICLESURROGATE PARKING_LOT [1000]; aircraft x; parking_lot [0] = x;

转载请注明原文地址:https://www.9cbs.com/read-7034.html

New Post(0)