头文件
/**
* 书本:【ThinkingInC++】
* 功能:重载的例子
* 时间:2014年8月28日16:03:37
* 作者:cutter_point
*/
#ifndef STASH3_H_INCLUDED
#define STASH3_H_INCLUDED
class 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;
}