C++沉思录

C++沉思录

第五章 代理类 :

为了实现容器或数组的多态性。

 1 #include <iostream>
2
3 using namespace std;
4
5 class Vehicle
6 {
7 public:
8 virtual void start() const = 0 ;
9 virtual Vehicle* copy() const = 0 ;
10 virtual ~Vehicle() {};
11 };
12
13
14 class AutoVehicle : public Vehicle
15 {
16 public:
17 void start() const
18 {
19 cout << "start " << endl;
20 }
21 Vehicle* copy() const
22 {
23 return new AutoVehicle(* this );
24 }
25 ~AutoVehicle(){}
26 };
27
28 class Car : public AutoVehicle
29 {
30 public:
31 void start() const
32 {
33 cout << " car start " << endl;
34 }
35 Vehicle* copy() const
36 {
37 return new Car( *this );
38 }
39 ~Car(){}
40 };
41
42 class Truck : public AutoVehicle
43 {
44 public:
45 void start() const
46 {
47 cout << " truck start " << endl;
48 }
49 Vehicle* copy() const
50 {
51 return new Truck( *this );
52 }
53 ~Truck(){};
54 };
 1 #include "vehicle.h"
2
3 class VehicleSurrogate
4 {
5 public :
6 VehicleSurrogate();
7 VehicleSurrogate( const Vehicle&);
8 VehicleSurrogate( const VehicleSurrogate&);
9 VehicleSurrogate& operator= ( const VehicleSurrogate&);
10 ~VehicleSurrogate();
11 void start();
12 private:
13 Vehicle* vp;
14 };
15 VehicleSurrogate::VehicleSurrogate() : vp ( NULL){}
16 VehicleSurrogate::VehicleSurrogate(const Vehicle& v)
17 {
18 vp = v.copy();
19 }
20 VehicleSurrogate::VehicleSurrogate(const VehicleSurrogate& v) : vp(v.vp ? v.vp->copy() : NULL) {};
21 VehicleSurrogate& VehicleSurrogate::operator=( const VehicleSurrogate& v)
22 {
23 if ( this != &v )
24 {
25 delete vp;
26 vp = (v.vp ? v.vp->copy() : NULL);
27 }
28 return *this;
29 }
30 void VehicleSurrogate::start()
31 {
32 if ( vp == NULL )
33 throw "empty";
34
35 vp->start();
36 }
37 VehicleSurrogate::~VehicleSurrogate()
38 {
39 delete vp;
40 }
 1 #include "VehicleSurrogate.h"
2
3 int main()
4 {
5
6 VehicleSurrogate parking_lot[100];
7 Car x;
8 Truck t;
9 parking_lot[0] = VehicleSurrogate( x);
10 parking_lot[1] = VehicleSurrogate( t);
11 parking_lot[0].start();
12 parking_lot[1].start();
13
14 return 0 ;
15 }





posted on 2011-12-28 00:36 lzhenf 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lzhenf/archive/2011/12/28/2304191.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/404550.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VC内存泄露检查工具:VisualLeakDetector

From: http://www.xdowns.com/article/170/Article_3060.html 初识Visual Leak Detector 灵活自由是C/C语言的一大特色&#xff0c;而这也为C/C程序员出了一个难题。当程序越来越复杂时&#xff0c;内存的管理也会变得越加复杂&#xff0c;稍有不慎就会出现内存问题。内…

JQuery Easy Ui 可装载组合框 - ComboBox

JQuery Easy Ui 可装载组合框 - ComboBox 可装载组合框 - ComboBox 继承自$.fn.combo.defaults,通过$.fn.combobox.defaults覆盖默认值 combobox显示的是一个可以编辑的文本框和一个下拉列表.允许用户从里面选择一个或者是多个值,用户可以直接输入值到列表顶部,或者可以从列表选…

提高 Web 站点性能的最佳实践

原文地址&#xff1a;http://developer.yahoo.com/performance/rules.html 本文在尊重原文基础上&#xff0c;尽量翻译得通俗易懂一些。 本文内容 提高 Web 站点性能的最佳实践最大限度减少 HTTP 请求 使用内容分发网络&#xff08;CDN&#xff09; 添加 Expires 或 Cache – C…

内存泄露检测工具比较

From: http://blog.163.com/zhuang_qianxin/blog/static/29765138201051092529107/ 1. ccmalloc&#xff0d;Linux和Solaris下对C和C程序的简单的使用内存泄漏和malloc调试库。 2. Dmalloc&#xff0d;Debug Malloc Library. 3. Electric Fence&#xff0…

用SQL实现统计报表中的“小计”和“合计”

客户提出需求&#xff0c;针对某一列分组加上小计&#xff0c;合计汇总。网上找了一些有关SQL加合计的语句。都不是很理想。决定自己动手写。 思路有三个&#xff1a; 1.很多用GROUPPING和ROLLUP来实现。 优点实现代码简洁&#xff0c;要求对GROUPPING和ROLLUP很深的理解。 …

Linux时间函数

From: http://blog.csdn.net/water_cow/article/details/7521567 系统环境&#xff1a;ubuntu10.04简介本文旨在为了解Linux各种时间类型与时间函数提供技术文档。1、Linux下常用时间类型Linux下常用时间类型有四种&#xff1a;time_t、struct tm、struct timeval、struct ti…

Ubuntu安装BackExec Remote Agent for Linux

把那个RALUS_RMALS_RAMS-4164.5.tar.gz的安装包传输至服务器安装&#xff0c;注意一点&#xff0c;需要新建个目录&#xff0c;把该文件放进去&#xff0c;否则&#xff0c;该文件直接解压的话&#xff0c;将在该目录生成一大堆文件。&#xff08;symantec怎么会这么制作安装包…

Linux C函数之时间函数

From: http://school.cfan.com.cn/system/unix/2009-07-01/1246421513d214853.shtml 时间函数(13) 函数分类: 1. 设置时间: settimeofday, tzset 2. 获取时间: time, ftime, gettimeofday 3. 时间格式转换: mktime, strftime; gmtime, localtime; asctime, ctime 4. 其他: cl…

Linux串口驱动程序-termios结构体中的VTIME和VMIN应用

#Linux串口驱动程序-termios结构体中的VTIME和VMIN应用 参考 https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html 最近在看Unix-linux系统编程一书&#xff0c;做到第六章的时候需要做一个标准输入的超时处理。如下图所示 需要改动的&#xff0c;是原书的play_aga…

C# 线程手册 第一章 线程定义 .NET 和 C# 对线程的支持

由于.NET Framework 支持自由线程&#xff0c;所以自由线程在所有.NET 语言中都存在&#xff0c;包括C#和VB.NET. 在下一部分&#xff0c;我们将着重关注如何提供这种支持以及更多关于线程是如何做到的&#xff0c;而不再关注线程是什么。我们将讨论一些能够进一步帮助区分进程…

SAFEARRAY使用实例

From: http://blog.csdn.net/csfreebird/article/details/234547 目录&#xff1a;SAFEARRAY使用实例目录&#xff1a;前言&#xff1a;何谓SAFEARRAY&#xff1a;创建SAFEARRAY:方法一&#xff1a;使用SafeArrayAllocDescriptor在栈上创建一维数组方法二&#xff1a;使用Safe…

前端第一天 HTML基础

前端第一天 HTML基础 1.是什么 HTML里值得记住的就几个点&#xff0c;第一&#xff0c;这东西还有自身也有结构 这东西是个超文本&#xff0c;可以挂载文字图片视频或者别的超文本自身可以通过各种各样的tag进行标记&#xff0c;排版给浏览器提供渲染的依据&#xff0c; 2.…

把一个结构体当做属性后碰到的问题

当我把一个"结构体"在类中当做属性后, 在实用中可以直接读取结构体成员, 但不能直接写入...下面是由此引发的小练习:unit Unit1;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.…

VC6 + OpenCV1.0实现图片缩放显示

用vc6新建一个win32控制台程序&#xff0c;代码&#xff1a; /*功能&#xff1a;实现加载jpg图片&#xff0c;并进行缩放显示开发环境: winXP vc6 openCV1.0头文件路径:D:\opensource\opencv1.0\cv\includeD:\opensource\opencv1.0\cxcore\includeD:\opensource\opencv1.0\ot…

view2.0移植自定义图标,带颜色修改

1.下载你的iconfont项目 1.将图标添加到项目&#xff0c;修改font-family值 2.下载项目打包文件&#xff0c;解压后如图所示 我们只关心里面的iconfont.css和iconfont.json 我们在这个文件夹&#xff0c;新建一个convert.js,内容如下 //convert.js let path1 "./iconf…

[react] react中你用过哪些第三方的中间件

[react] react中你用过哪些第三方的中间件 redux-thunk: Redux的异步处理方案,actionCreator中可以返回一个函数&#xff08;即可以dispatch一个function&#xff09;&#xff0c;函数内在写异步的代码redux-saga: Redux的异步处理方案&#xff0c;没有破坏redux中dispatch一个…

从PeopleEditor控件中取出多用户并更新到列表

如果一个列表中有一个字段类型为用户或用户组&#xff0c;并且设置为用户&#xff0c;允许多值的话&#xff0c;那么用代码进行更新的时候就必须将这个字段的值赋成SPFieldUserValueCollection类型&#xff0c;以下代码即为从PeopleEditor控件中取出多个用户并返回一个SPFieldU…

[react] 怎样在react中使用innerHTML?

[react] 怎样在react中使用innerHTML&#xff1f; 使用dangerouslySetInnerHTML属性,该属性传入一个对象&#xff0c;对象中__html属性的值即时innerHTML的富文本代码 个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。…

vc++出现warningC4819的处理方法

From: http://blog.sina.com.cn/s/blog_93e339ba01014fiw.html 编译VC程序的时候出现如下提示警告&#xff1a; warning C4819: The file contains a character that cannot berepresented in the current code page (936). Save the file inUnicode format to prevent data l…

H3C——路由策略和策略路由实例配置

配置如下&#xff1a;[IP]int s0/2/0[IP-Serial0/2/0]ip add 202.112.1.10 28[IP-Serial0/2/0]int s0/2/1[IP-Serial0/2/1]ip add 61.67.1.10 28 [IP-Serial0/2/1]int lo0[IP-LoopBack0]ip add 10.10.10.10 32[IP]ip route-static 0.0.0.0 0 202.112.1.9 指条静态缺省路由到R1 …