C++沉思录之代理

此文记录《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();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值