📄 example1-23.cpp
字号:
template <class AType>
class array
{
public:
array(int size); //构造函数
~array() //析构函数
{
delete []a;
}
AType &operator [](int i);//重载运算符[]
private:
int len;
AType *a;
};
template <class AType>
array <AType>::array(int size) //构造函数的定义
{
register I;
len=size;
a=new AType[size];
if(!a) //初始化出错
{
cout<<"cannot allocate array\n";
exit(1);
}
for(I=0;I<size;I++)
a[I]=0
}
template <class AType>
AType &array <AType>::operator[](int i) //重构运算符函数的定义
{
if(i<0||i>len-1) //出界错误
{
cout<<"\n Index value of";
cout<<i<<"is out of bounds\n";
exit(2);
}
return a[i];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -