这道题目最然不是很难,也折腾了挺久,
二维数组的创建需要定义一个指向指针的指针 int**p;重载下标符号之后,p[i]成了行入口地址,与一维数组数组名同意 , p[i][j] 可直接取元素;
类中声明指针作为数据成员,应在构造函数予以定义!
private:
int **p;
int hang,lie;
public:
Array2(){
p=NULL;
}
Array2(int a,int b){
cout<<"hah"<<endl;
hang = a;
lie = b;
p = new int*[a];
for(int i=0;i<a;++i){
p[i] = new int[b];
}
}
~Array2(){
for(int i=0;i<hang;++i){
delete [] p[i];
}
delete p;
}
int* operator[](int n){
return p[n];
}
int operator()(int a,int b){
return p[a][b];
}
Array2& operator=(Array2 &a)
{
if(p)
{
for(int i=0;i<hang;++i)
{
if(p[i])
delete [] p[i];
}
delete p;
}
hang = a.hang;
lie = a.lie;
p = new int*[hang];
for(int i=0;i<hang;++i)
{
p[i] = new int[lie];
for(int j=0;j<lie;++j)
{
p[i][j]=a.p[i][j];
}
}
return *this;
}