safearray.h
来自「数据结构ssd5 re3」· C头文件 代码 · 共 54 行
H
54 行
/*
* 063420 魏政
*/
#ifndef SAFEARRAY_H
#define SAFEARRAY_H
template <typename T>
class safearray {
private:
T *storage;
int capacity;
public:
safearray() : storage(NULL), capacity(0) {} // default constructor
//构造函数重载
safearray(int a)
{
capacity=a;
storage=new T[a];
}
//析构函数,删除所创建动态数组内存空间
~safearray(void)
{
if(storage!=NULL)
{
delete [] storage;
storage=NULL;
}
}
T& operator[] (int i) throw(out_of_range)
{
//如果choice大于10,抛出异常"Index is too high"
if(i >= capacity)
{
throw out_of_range("Index is too high\n");
}else if(i < 0)//若果choice小于0,抛出异常"Index is below 0"
{
throw out_of_range("Index is below 0\n");
}
return *(storage+i);
}
};
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?