面向对象的四个特征:抽象、继承、封装和多态性。 编写程序时我们常常要解决不同类型(封装)但彼此相关(继承)的功能。将继承和封装共用,迫使我们要处理两个问题,控制内存分配和把不同类型的对象放入同一个封装中。 例如,书中提到了这么一个例子: parking_lot――停车场;vehicle――停车场里的各种车辆; class Vehicle { public: virtual double weight () const = 0; virtual void start () = 0; }; class RoadVehicle: public Vehicle {/*…*/}; class AutoVehicle: public RoadVehicle {/*…*/}; class Aircraft: public Vehicle {/*…*/}; class Helicopter: public Aircraft {/*…*/}; 这里parking_lot不能用数组来实现,因为成员函数weight()和start()都是纯虚函数,类Vehicle本身就不会有对象,也就没有其对象数组。 但即使存在类Vehicle的对象(剔除了类vehicle中的所有纯虚函数),如果写类似下面的语句: Automobile x = /*…*/ parking_lot[num_vehicles++] = x; 很明显,parking_lot还仅仅是vehicles的集合。 如何解决这两个问题,有下面两种方法: 1.提供一个indirection。合适的indirection形式就是存储指针,如: vehicle * parking_lot[100]; parking_lot[num_vehicles++] = new Automobile(x); 2.使用一种被作者叫做surrogate的类,这个类的每个对象都代表另一个对象,可以是位于一个完整继承层次中的任意类的对象。 首先是使用indirection形式,但这种方式有两点不足: 1.带来了动态内存管理的负担 2.只有放到parking_lot中的对象的是静态类型时,这种方法才能用。否则,不能保证这个程序不会出错。假设让parking_lot[p]指向一个新建的vehicle,其类型和值要与由parking_lot[q]指向的对象相同。下面的两种方法都是错误的 // parkint_lot[p]和parkint_lot[q]将指向相同的对象。 if(p!=q) { delete parking_lot[p]; parking_lot[p] = parking_lot[q]; } // 没有vehicle类型的对象,即使有,也不是我们想要的! if(p!=q) { delete parking_lot[p]; parking_lot[p] = new vehicle(parking_lot[q]); }
然后是使用surrogate。这是用类来表示概念,Koenig和Moo把这一点当作基本的C++设计原则。 定义一个surrogate,行为和vehicle对象相似,而又潜在地表示了所有继承自vehicle类的对象的东西。这样,复制surrogate就会复制其相应的vehicle对象,给surrogate赋新值也会先删除旧对象,再复制新对象。 DagBruck指出,这种处理方式与我们所预期的赋值行为稍微有所不同,因为这种方式改变了左侧的代理类实际关联的那个对象的类型。 这里有几种要注意的: 1.不能缺省构造函数。因为如果vehicle是个抽象基类,我们就无法规定VehicleSurrogate的缺省操作和它所指向的对象的类型。不可能是类Vehicle,因为根本就没有Vehicle对象。引入行为类似于空指针的empty surrogate的概念。能够创建、销毁和复制这样的surrogate,但进行其他操作就视为出错。 2.每次对copy的调用都是一个虚拟调用,只能是虚拟的。 3.关于复制构造函数和赋值操作符中的v.vp非零的检测,这是必需的,否则调用v,vp->copy时就会出错 4.要对赋值操作符进行检测,确保没有将代理赋值给它自身。 5.令该surrogate类支持类Vehicle所能支持的其他操作。但由于surrogate类没有继承自该类的对象,所以这些函数都不是虚拟的。 6.这些函数本身可以调用相应Vehicle对象中的虚函数。它们也应该检查确保vp不为零 下面是问题的解决: class Vehicle { public: virtual double weight () const = 0; virtual void start () = 0; virtual Vehicle * copy () const = 0; virtual ~Vehicle() {} //处理完一个对象后,需要清除该对象。 //…… }; class RoadVehicle: public Vehicle {/*…*/}; class AutoVehicle: public RoadVehicle {/*…*/}; Vehicle * AutoVehicle::copy() const { return new AutoVehicle(*this); } class vehicleSurrogate{ public: //这样就能够创建VehicleSurrogate对象的数组了;empty surrogate VehicleSurrogate():vp(0) {}; //为能给任意继承自Vehicle的类的对象创建代理;empty surrogate VehicleSurrogate(const Vehicle&) :vp(v.copy()) {}; ~VehicleSurrogate(); VehicleSurrogate(const VehicleSurrogate&); VehicleSurrogate& operator=(const VehicleSurrogate&); Private: Vehicle *vp; }; VehicleSurrogate::VehicleSurrogate(const VehicleSurrogate&) : vp(v.vp? v.vp->copy() : c) {} VehicleSurrogate ::~VehicleSurrogate() { delete vp; } VehicleSurrogate& operator=(const VehicleSurrogate&) { if(this!= &v) { delete vp; vp = v.vp ? v.vp->copy(): 0); } return *this; } double VehicleSurrogate::weight() const { if (vp == 0) throw “empty VehicleSurrogate.weight()” return vp->weight(); } void VehicleSurrogate::start() { if (vp == 0) throw “empty VehicleSurrogate.start()”; vp->start(); } 停车场(parking_lot)就可以这样定义了: VehicleSurrogate parking_lot[1000]; AutoVehicle x; parking_lot[num_vehicles++] = x;

|