C++ 代理类的实现
来源:互联网 发布:使命召唤10 知乎 编辑:程序博客网 时间:2024/06/05 21:03
#include <string>#include <iostream>class Vehicle{protected: std::string name;public: Vehicle():name("noName"){}; Vehicle(const std::string& s):name(s){}; virtual Vehicle * clone() const = 0; virtual void run() const = 0; virtual ~Vehicle(){std::cout <<" Base destruction!" << std::endl;}};class Car :public Vehicle{public: Car():Vehicle("car"){}; Car(const std::string & s):Vehicle(s){}; void run() const{std::cout << name << " is running" << std::endl;} Car * clone()const {return new Car("car");} ~Car(){std::cout <<" Car destruction!" << std::endl;}};class Truck: public Vehicle{public: Truck():Vehicle("truck"){}; Truck(const std::string & s):Vehicle(s){}; void run() const{std::cout << name << " is running" << std::endl;} Truck * clone()const {return new Truck("truck");} ~Truck(){std::cout <<" Truck destruction!" << std::endl;}};class AirPlane:public Vehicle{public: AirPlane():Vehicle("airPlane"){}; AirPlane(const std::string & s):Vehicle(s){}; void run() const{std::cout << name << " is running" << std::endl;} AirPlane * clone()const {return new AirPlane("airplane");} ~AirPlane(){std::cout <<" AirPlane destruction!" << std::endl;}};class VehicleProxy{private: Vehicle *ve;public: VehicleProxy():ve(0){} VehicleProxy(const Vehicle & v):ve(v.clone()){} VehicleProxy(const VehicleProxy &vs):ve(vs.ve ? vs.ve->clone() : 0){} VehicleProxy & operator = (const VehicleProxy & vs); ~VehicleProxy(){delete ve;} void run(){if(ve != 0) ve->run();}};VehicleProxy & VehicleProxy::operator = (const VehicleProxy & vs){ if(&vs != this) { delete ve; ve = vs.ve->clone(); } return *this;}int main(){VehicleProxy vehicleProxy[3]; Car car; Truck truck; AirPlane airPlane; vehicleProxy[0] = car; vehicleProxy[1] = truck; vehicleProxy[2] = airPlane; for(int i = 0; i < 3; i++) vehicleProxy[i].run(); return 0;}
0 0
- 代理类的实现
- 安卓so动态库加载代理实现,可以实现C层的类反射效果
- C#HTTP代理的实现之注册表实现
- C++ 代理类的实现
- 设计模式--代理模式的简单实现(C#)+类关系图
- more effective c++——Item M30 代理类(一)多维数组的实现
- cglib动态代理[基于类操作的动态代理实现]
- 类型的代理类(c++)
- C++代理类的使用
- 代理类:接口与实现的分离
- 模拟JDK动态代理类的实现
- java的反射实现动态代理类
- java动态代理类的实现
- 实现生成代理类的原理
- 动态代理的实现
- SOCKS代理的实现
- 协议代理的实现
- 动态代理的实现
- Window下设置Octave
- HDFS Federation(HDFS 联邦)(hadoop2.3)
- QQ浏览器上线安全联盟认证 保护网购安全
- C字符串处理函数
- libgdx中利用打包工具TexturePacker打包游戏图片节约内存
- C++ 代理类的实现
- 多线程的那点儿事(之数据互斥)
- spring test
- hdu1009简单的贪心问题
- iOS开发-苹果开发者账号注册、申请续费整个流程
- 对信号与系统中卷积的理解
- Android中的EditText默认时不弹出软键盘的方法
- QQ浏览器上线安全联盟认证 保护网购安全
- MFC中头文件和预定义宏