c++代理类

C++代理类是为了解决这样的问题: 容器通常只能包含一种类型的对象,所以很难在容器中存储对象本身。怎样设计一个c++容器,使它有能力包含类型不同而彼此相关的对象? 

代理运行起来和他所代表的对象基本相同,但是允许将整个派生层次压缩在一个对象类型中。

[cpp]  view plain  copy
 print ?
  1. #include <iostream>  
  2. using namespace std;  
  3. //定义基类,纯虚函数  
  4. class Vehicle  
  5. {  
  6.     public:  
  7.         virtual double weight() const = 0;  
  8.         virtual Vehicle* copy() = 0;  
  9.         virtual ~Vehicle() {}  
  10. };  
  11. //定义1  
  12. class AutoVehicle:public Vehicle  
  13. {  
  14.     public:  
  15.         double weight() const  
  16.         {  
  17.             cout << "AutoVehicle weight" << endl;  
  18.             return 1;  
  19.         }  
  20.         Vehicle* copy()//主要是这里,因为没有RoadVehicle(vehicle)这样的构造函数  
  21.         {  
  22.             return new AutoVehicle(*this);//调用了默认的拷贝构造函数   
  23.         }  
  24. };  
  25. //定义2  
  26. class RoadVehicle:public Vehicle  
  27. {  
  28.     public:  
  29.         double weight() const  
  30.         {  
  31.             cout << "RoadVehicle weight" << endl;  
  32.             return 2;  
  33.         }  
  34.         Vehicle* copy()  
  35.         {  
  36.             return new RoadVehicle(*this);  
  37.         }  
  38. };  
  39. //定义代理类,通过这个类来访问不同对象的方法。  
  40. class VehicleSurrogate  
  41. {  
  42.     public:  
  43.         VehicleSurrogate();  
  44.         VehicleSurrogate(Vehicle&);  
  45.         ~VehicleSurrogate();  
  46.         VehicleSurrogate(const VehicleSurrogate&);  
  47.         VehicleSurrogate& operator=(const VehicleSurrogate&);  
  48.         double weight() const;  
  49.     private:  
  50.         Vehicle *vp;  
  51. };  
  52. VehicleSurrogate::VehicleSurrogate():vp(0){} //空代理  
  53. //调用对象的copy函数,即构造对象的一个副本  
  54. VehicleSurrogate::VehicleSurrogate(Vehicle &v):vp(v.copy()){}  
  55. VehicleSurrogate::~VehicleSurrogate()  
  56. {  
  57.     delete vp;  
  58. }  
  59. //如果对象是代理类,也是可以复制其代理的对象的副本。  
  60. VehicleSurrogate::VehicleSurrogate(const VehicleSurrogate &v):vp(v.vp ? v.vp->copy() : 0){}  
  61. VehicleSurrogate& VehicleSurrogate::operator=(const VehicleSurrogate &v)  
  62. {  
  63.     if(this != &v)  
  64.     {  
  65.         delete vp;  
  66.         vp = (v.vp ? v.vp->copy() : 0);  
  67.     }  
  68.     return *this;  
  69. }  
  70. double VehicleSurrogate::weight() const  
  71. {  
  72.     if(vp == 0)  
  73.     {  
  74.         cout << "empty VehicleSurrogate" << endl;  
  75.     }  
  76.     else  
  77.     {  
  78.         return vp->weight();  
  79.     }  
  80. }  
  81. int main()  
  82. {  
  83.     VehicleSurrogate parking_lot[1000];  
  84.     AutoVehicle x;  
  85.     int num_vehicles = 0;  
  86.     parking_lot[num_vehicles++] = x;  
  87.     parking_lot[0].weight();  
  88.     RoadVehicle y;  
  89.     parking_lot[num_vehicles++] = VehicleSurrogate(y);  
  90.     parking_lot[1].weight();  
  91.     return 0;  
  92. }  
为了避免显示的处理内存分配,又能保持类Vehicle在运行时绑定。这里采用代理类技术,就是定义一个行为和Vehicle对象相似,而又潜在地表示所有继承自Vehicle类的对象的东西。每个Vehicle代理都代表某个继承自Vehicle类的对象。只要该代理关联着这个对象,该对象就肯定存在。

代理类的每个对象都代表另一个对象,该对象可以是位于一个完整继承层次中的任何类的对象。通过在容器中使用代理对象而不是对象本身的方式,就是代理类的精髓思想所在。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值