【ThinkingInC++】35、重载的例子
来源:互联网 发布:phpmyadmin版本与mysql 编辑:程序博客网 时间:2024/05/03 11:44
头文件
/*** 书本:【ThinkingInC++】* 功能:重载的例子* 时间:2014年8月28日16:03:37* 作者:cutter_point*/#ifndef STASH3_H_INCLUDED#define STASH3_H_INCLUDEDclass Stash{ int size; //表示要保存的数据占用的字节个数 int quantity; //总的内存块数目 int next; //已经存放的对象个数,用来指示下一个空的地方在哪里 //动态申请数组大小是字节 unsigned char* storage; void inflate(int increase); //这个只被add函数调用,属于内部实现的部分,用来增加内存public: Stash(int size); //0的起始内存块数目 Stash(int size, int initQuantity); //函数重载 ~Stash(); int add(void* element); void* fetch(int index); int count();};#endif // STASH3_H_INCLUDED
定义文件
/*** 书本:【ThinkingInC++】* 功能:重载的例子,头文件的定义* 时间:2014年8月28日16:03:42* 作者:cutter_point*/#include"Stash3.h"#include"../require.h"#include<iostream>#include<cassert>using namespace std;const int increment=100; //每次增加的内存基本数目/*int size; //表示一个对象占用的字节个数int quantity; //总的内存块数目int next; //已经存放的对象个数,用来指示下一个空的地方在哪里//动态申请数组大小是字节void inflate(int increase); //这个只被add函数调用,属于内部实现的部分,用来增加内存公有部分:Stash(int sz); //0的起始内存块数目Stash(int sz, int initQuantity); //函数重载~Stash();int add(void* element);void* fetch(int index);int count();*///void inflate(int increase);//这个只被add函数调用,属于内部实现的部分,用来增加内存void Stash::inflate(int increase)//increase内存大小,增加内存的分配大小{ assert(increase >= 0); if(increase == 0) return; int newQuantity=quantity+increase; //这个是新的大小,旧的加要增加的 int newBytes=newQuantity*size; //字节数(内存块数*每块的大小) int oldBytes=quantity*size; unsigned char* b=new unsigned char[newBytes]; //newBytes字节数对应的空间 //吧旧的空间数据放到新的上去 for(int i=0 ; i < oldBytes ; ++i) b[i]=storage[i]; //回收旧的空间 delete [] storage; //吧storage指向新的空间位置,!!!!2014年8月12日23:18:33这里错了检查半天 storage=b; //得到新的总的内存块数目 quantity=newQuantity;}//Stash(int size); //0的起始内存块数目Stash::Stash(int sz){ size=sz; quantity=0; next=0; storage=0;}//Stash(int sz, int initQuantity); //函数重载Stash::Stash(int sz, int initQuantity){ size=sz; quantity=0; //这里还是0,因为还没有给他分配空间 next=0; storage=0; //通过调用函数来分配空间 inflate(initQuantity);}//~Stash();Stash::~Stash(){ if(storage != 0) { cout<<"freeing storage"<<endl; delete []storage; }}//int add(void* element);int Stash::add(void* element){ if(next >= quantity) //起始空余的地方比总内存还要大 inflate(increment); //增加内存 int startBytes=next*size; //全部已经占用的内存 unsigned char* e=(unsigned char*)element; for(int i=0 ; i < size ; ++i) { storage[startBytes+i]=e[i]; //加到已经有的后面 } next++; //表示第一个空内存的位置 return (next-1); //返回对象的个数}//void* fetch(int index);void* Stash::fetch(int index){ require(0 <= index, "Stash::fetch (-)index"); if(index >= next) return 0; //没有对应的值 return &(storage[index*size]); //返回第index个的对象}//int count();int Stash::count(){ return next; //内存数据的总数}
测试文件
/*** 书本:【ThinkingInC++】* 功能:测试重载* 时间:2014年8月28日16:03:48* 作者:cutter_point*/#include"Stash3.cpp"#include<fstream>#include<string>int main(){ Stash intStash(sizeof(int)); //cout<<"!!"<<endl; for(int i=0 ; i < 100 ; ++i) intStash.add(&i); //吧数据带入对象 //void* Stash::fetch(int index) for(int j=0 ; j < intStash.count() ; ++j) cout<<"intStash.fetch("<<j<<")=" <<*(int*)intStash.fetch(j)<<endl; const int bufsize=80; Stash stringStash(sizeof(char)*bufsize, 100); //重载函数 ifstream in("Stash3Test.cpp"); assure(in, "Stash3Test.cpp"); string line; while(getline(in, line)) stringStash.add((char*)line.c_str()); //吧string类型转换成char* int k=0; char* cp; //void* Stash::fetch(int index) while((cp=(char*)stringStash.fetch(k++)) != 0) cout<<"stringStash.fetch("<<k<<")=" <<cp<<endl; return 0;}
0 0
- 【ThinkingInC++】35、重载的例子
- 【ThinkingInC++】64、重载new和delete,来模仿内存的分配
- 重载的例子.....简单
- 方法重载的例子
- 方法的重载例子
- 重载的例子
- 【ThinkingInC++】38、选择重载还是默认参数
- 【ThinkingInC++】45、比较const和非const成员函数的例子
- 【ThinkingInC++】3、文件的拷贝
- 【ThinkingInC++】10、全局变量的使用
- 【ThinkingInC++】28、public的作用
- 【ThinkingInC++】60、嵌入的迭代器
- 关于equals()的重载例子
- JAVA方法重载的例子
- operator<<的重载,一个例子
- c++ operator重载的例子
- c++ operator重载的例子
- 重载++运算符的例子
- C++学习笔记 指针与堆
- Maven + myeclise集成,及web项目创建,和导入现成的web项目
- oracle on linux 巡检脚本-部分
- 解决从tomcat和Resin服务器下载apk,ipa文件自动转化为zip的问题
- Profile your applications with Java VisualVM
- 【ThinkingInC++】35、重载的例子
- 流特殊函数操作
- Struts标签 s:url传递参数s:param 只能传递一个参数
- activity切换效果theme分析
- [转]MyEclipse内存不足问题
- VI 主要操作
- myeclise maven导入现成的web项目时出现的问题 增加web支持
- lua-5.2的变化(持续更新)
- centos安装ftp服务器