此文记录《C++沉思录》 一书中的代理章节中的代码。
/******************************************************************************
Module: surrogate.h
Purpose: 代理是为了解决继承和容器问题而提出的。容器只能容纳同种类型的对象。
而在继承层次里面有多种变量类型,虽然他们相互关联,也不能找到一个很
有效的办法将之放入容器,传统的方法是以指针作为中间层,在容器内部存
储指针,而这样又会带来内存管理的麻烦。在C++里面,通常采用代理的方式
解决这一问题,将代理作为一个中间层。
Note: 注意空指针的检测,注意const引用参数的使用,注意自我赋值的检测
内容参照Andrew Koenig 和 Barbara Moo 的 《C++沉思录》
******************************************************************************/
#ifndef _SURROGATE_H
#define _SURROGATE_H
#include <iostream>
// the base class
class Vehicle {
public:
virtual double weight() const = 0;
virtual void start() = 0;
virtual Vehicle* copy() const = 0;
virtual ~Vehicle() { std::cout << "~Vehicle()" << std::endl; }
//...
};
class RoadVehicle: public Vehicle {
public:
double weight() const;
void start();
Vehicle* copy() const;
~RoadVehicle() { std::cout << "~RoadVehicle()" << std::endl; }
/*...*/};
class AutoVehicle: public RoadVehicle {
public:
double weight() const;
void start();
Vehicle* copy() const;
~AutoVehicle() { std::cout << "~AutoVehicle()" << std::endl; }
/*...*/
};
//class Aircraft: public Vehicle { /* ... */ };
//class Helicopter: public Aircraft { /* ... */ };
class VehicleSurrogate{
public:
VehicleSurrogate();
~VehicleSurrogate();
VehicleSurrogate(const Vehicle & v);
VehicleSurrogate(const VehicleSurrogate & vs);
VehicleSurrogate& operator=(const VehicleSurrogate& vs);
//methods from class Vehicle
double weight() const;
void start();
private:
Vehicle* vp;
};
#endif
/******************************************************************************
Module: surrogate.cpp
******************************************************************************/
#include <iostream>
#include "surrogate.h"
using namespace std;
double RoadVehicle::weight() const
{
return 200.0;
}
void RoadVehicle::start()
{
cout << "RoadVehicle start" << endl;
}
Vehicle* RoadVehicle::copy() const
{
return new RoadVehicle(*this);
}
double AutoVehicle::weight() const
{
return 100.0;
}
void AutoVehicle::start()
{
cout << "AutoVehicle start" << endl;
}
Vehicle* AutoVehicle::copy() const
{
return new AutoVehicle(*this);
}
VehicleSurrogate::VehicleSurrogate():vp(0){}
VehicleSurrogate::~VehicleSurrogate()
{
delete vp;
cout << "~VehicleSurrogate()" << endl;
}
VehicleSurrogate::VehicleSurrogate(const Vehicle &v)
:vp(v.copy()){}
VehicleSurrogate::VehicleSurrogate(const VehicleSurrogate & vs)
:vp(vs.vp ? vs.vp->copy() : 0){}
VehicleSurrogate& VehicleSurrogate::operator=(const VehicleSurrogate& vs)
{
if(&vs != this) // check for if we assign itself
{
delete vp;
vp = vs.vp ? vs.vp->copy() : 0;
}
return *this;
}
double VehicleSurrogate::weight() const
{
if(vp == 0) // check for null pointer
{
throw "Empty VehicleSurrogate.weight()";
}
return vp->weight();
}
void VehicleSurrogate::start()
{
if(vp == 0) // check for null pointer
{
throw "Empty VehicleSurrogate.start()";
}
return vp->start();
}