C++编程思想第三章例子代码(三)句柄

本文介绍了一种C++中使用句柄类管理资源的方法。通过定义一个句柄类handle来间接操作内部资源,该资源由一个指向结构体cheshire的指针smile管理。文章详细展示了句柄类的声明、定义及如何初始化、清理资源,同时提供了完整的示例代码。

关键词calss(类)与struct作用相近,但缺省关键词不同

类class(缺省类型为private)

struct (缺省类型为public)

句柄 类(handle class)的作用:便于修改一个类,减少重复编译

//例子中,头文件中只包含公共接口和一个简单的未完全指定的类指针
//:HANDLE.H--Handle classes
#ifndef HANDLE_H_
#define HANDLE_H_
	class handle{
		struct cheshire;
		cheshire* smile;
	public:
		void initialize();
		void cleanup();
		int read();
		void change(int);
	};
#enddef

//:HANDLE.CPP-Handle implementation
#include"handle.h"
#include<stdlib.h>
#include<assert.h>
//define handle's implementation
struct handle::cheshire{            //对类指针按照需求进行定义
	int i;
};
void handle::initialize(){
	smile=(cheshire*)malloc(sizeof(cheshire));
	assert(smile);
	smile->i=0;
}
void handle::cleanup(){
	free(smile);
}
int handle::read(){
	return smile->i;
}
void handle::change(int x){
	smile->i=x;
}

//:USEHANDLE.CPP--use the handle class
#include<handle.cpp>
main(){
	handle u;
	u.initialize();
	u.read();
	u.change(1);
	u.cleanup();
}


C++编程思想代码用于编程思想,会给大家带来帮助 Copyright (c) 2000, Bruce Eckel Source code file from the book "Thinking in C++" All rights reserved EXCEPT as allowed by the following statements: You can freely use this file for your own work (personal or commercial), including modifications and distribution in executable form only. Permission is granted to use this file in classroom situations, including its use in presentation materials, as long as the book "Thinking in C++" is cited as the source. Except in classroom situations, you cannot copy and distribute this code; instead, the sole distribution point is http://www.BruceEckel.com (and official mirror sites) where it is available for free. You cannot remove this copyright and notice. You cannot distribute modified versions of the source code in this package. You cannot use this file in printed media without the express permission of the author. Bruce Eckel makes no representation about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty of any kind, including any implied warranty of merchantability, fitness for a particular purpose, or non-infringement. The entire risk as to the quality and performance of the software is with you. Bruce Eckel and the publisher shall not be liable for any damages suffered by you or any third party as a result of using or distributing this software. In no event will Bruce Eckel or the publisher be liable for any lost revenue, profit, or data, or for direct, indirect, special, consequential, incidental, or punitive damages, however caused and regardless of the theory of liability, arising out of the use of or inability to use software, even if Bruce Eckel and the publisher have been advised of the possibility of such damages. Should the software prove defective, you assume the cost of all necessary servicing, repair, or correction. If you think you've found an error, please submit the correction using the form you will find at www.BruceEckel.com. (Please use the same form for non-code errors found in the book.)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值