二维数组类关于深拷贝

wukele 2006-11-16 07:21:17
class Arrary
{public:
Arrary(int m=5,int n=6,int value=0)
{
arrary=new int*[m];
for(int i=0;i<m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
*(*(arrary+i)+j)=value;

}
Arrary( Arrary &other)
{this->~Arrary();
printf("copy");
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
printf("%d",other.arrary[i][j]); }
m=other.m;
n=other.n;
}
~Arrary()
{for(int i=0;i<m;i++)
delete[] arrary[i];
delete[] arrary;

}
int GetValue(int m,int n)
{if( m<0 || m>this->m || n<0 || n>this->n)
{printf("访问不存在的元素!");
return 0;}
else
{return arrary[m][n];}

}
int SetValue(int m,int n)
{}

private:
int m,n;
int **arrary;
};
using namespace std;
void test(Arrary a)
{cout<<"test:"<<a.GetValue(4,4)<<endl;}
int main(){
Arrary a(5,5,4),b;
b=a;
//test(a);
cout<<b.GetValue(4,4);
system("pause");
return 0;
}
==================
我写的深拷贝函数有错吗?为什么运行的时候会出错啊。。
...全文
288 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2006-11-17
  • 打赏
  • 举报
回复
建议使用vector<vector<> >或者boost::multiarray。
要自己写,也最好参考它们的源码。
OOPhaisky 2006-11-16
  • 打赏
  • 举报
回复
this->m=5;
this->n=n;
------------------------------------------------------------------------------
汗!!
刚才看了半天,竟然没有看出来,我就觉得是m,n还有分配释放的问题,但是没看出来,自己也惭愧啊!!
wukele 2006-11-16
  • 打赏
  • 举报
回复
不好意思啊。。是我自己的疏忽造成运行错误的。。我少写代码
现在把正确地贴上来
#include<iostream>
class Arrary
{public:
Arrary(int m=5,int n=6,int value=0)
{
arrary=new int*[m];
for(int i=0;i<m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
*(*(arrary+i)+j)=value;
this->m=5;
this->n=n;

}
Arrary( Arrary &other)
{//this->~Arrary();
printf("copy%l",other.m);
arrary=new int*[other.m];
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
}
m=other.m;
n=other.n;

}
// Arrary& operator =( const Arrary &other )
// { printf("copytret%d",other.m);}
~Arrary()
{for(int i=0;i<m;i++)
delete[] arrary[i];
delete[] arrary;

}
int GetValue(int m,int n)
{if( m<0 || m>this->m || n<0 || n>this->n)
{printf("访问不存在的元素!");
return 0;}
else
{return arrary[m][n];}

}
int SetValue(int m,int n)
{}

private:
int m,n;
int **arrary;
};
using namespace std;
void test(Arrary a)
{cout<<"test:"<<a.GetValue(4,4)<<endl;}
int main(){
Arrary a(5,5,4),b;
// b=a;
//Arrary b=Arrary(a);
test(a);
cout<<b.GetValue(4,4);
system("pause");
return 0;
}
wukele 2006-11-16
  • 打赏
  • 举报
回复
把上面的拷贝函数改为这个
Arrary( Arrary &other)
{
printf("copy");
arrary=new int*[other.m];
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
printf("%d",other.arrary[i][j]); }
m=other.m;
n=other.n;
}
运行的时候还是会出错
wukele 2006-11-16
  • 打赏
  • 举报
回复
把上面的拷贝函数改为这个
Arrary( Arrary &other)
{
printf("copy");
arrary=new int*[other.m];
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
printf("%d",other.arrary[i][j]); }
m=other.m;
n=other.n;
}
OOPhaisky 2006-11-16
  • 打赏
  • 举报
回复
好像是析构函数导致的运行时错误......
wukele 2006-11-16
  • 打赏
  • 举报
回复
不好意思,我忘记把this- >~Arrary()注释掉了。。
把它注释了。。但是还会出错~~
a_b_c_abc2 2006-11-16
  • 打赏
  • 举报
回复
拷贝构造是用一个已存在的对象去构造一个不存在的对象 。

//this->~Arrary(); 对象都没建立,析构个屁啊

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧