为深拷贝散分!

anything_is_possible 2008-04-06 04:39:58
#include<iostream>
using namespace std;
class point
{
public:
point()
{
x=y=0;
cout<<"default constructor called."<<endl;
}
point(int xx,int yy)
{
x=xx;
y=yy;
cout<<"constructor called."<<endl;
}
~point()
{
cout<<"destructor called."<<endl;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
void move(int j,int k)
{
x=j;
y=k;
}
private:
int x,y;
};
class arrayofpoints
{
public:
arrayofpoints(int n)
{
numberofpoints=n;points=new point[n];
}
arrayofpoints(arrayofpoints&pointsarray);
~arrayofpoints()
{
cout<<"deleting..."<<endl;
numberofpoints=0;
delete[] points;
}
point&element(int n)
{
return points[n];
}
private:
point *points;
int numberofpoints;
};
arrayofpoints:: arrayofpoints(arrayofpoints&pointsarray)
{
numberofpoints=pointsarray.numberofpoints;
points=new point[numberofpoints];
for(int i=0;i<numberofpoints;i++)
points[i].move(pointsarray.element(i).getx(),pointsarray.element(i).gety());
}
int main()
{
int number;
cout<<"please enter the number of points:";
cin>>number;
arrayofpoints pointsarray1(number);
pointsarray1.element(0).move(5,10);
pointsarray1.element(1).move(15,20);
arrayofpoints pointsarray2(pointsarray1);
cout<<"copy of pointsarray1:"<<endl;
cout<<"point_0 of array2:"
<<pointsarray2.element(0).getx()
<<","<<pointsarray2.element(0).gety()<<endl;
cout<<"point_1 of array2:"
<<pointsarray2.element(1).getx()
<<","<<pointsarray2.element(1).gety()<<endl;
pointsarray1.element(0).move(25,30);
pointsarray1.element(1).move(35,40);
cout<<"after the moving of pointsarray1:"<<endl;
cout<<"point_0 of array2"
<<pointsarray2.element(0).getx()
<<","<<pointsarray2.element(0).gety()<<endl;
cout<<"point_1 of array2:"
<<pointsarray2.element(1).getx()
<<","<<pointsarray2.element(1).gety()<<endl;
}
其中实现深拷贝那段有些看不懂,也就是
arrayofpoints:: arrayofpoints(arrayofpoints&pointsarray)
{
numberofpoints=pointsarray.numberofpoints;
points=new point[numberofpoints];
for(int i=0;i<numberofpoints;i++)
points[i].move(pointsarray.element(i).getx(),pointsarray.element
辛苦各位高手了!不要吝啬你们的文字!讲细点!
...全文
244 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
hui12345685 2008-04-07
  • 打赏
  • 举报
回复
jf
  • 打赏
  • 举报
回复
别给我霁概念!概念我知道!关于深拷贝实现的那段代码,我看的不大懂!我想请你们解释的就是那段代码,要讲清楚,那段代码是干什么的?有什么用!
不想低调 2008-04-07
  • 打赏
  • 举报
回复
jf
qinqinhao 2008-04-07
  • 打赏
  • 举报
回复
學習
xbt746 2008-04-07
  • 打赏
  • 举报
回复
浅拷贝指在拷贝构造中,直接将指针的地址给了另外一个对象
深拷贝是把指针所指向的值赋值给另外一个对象
DL88250 2008-04-07
  • 打赏
  • 举报
回复
路过,up
paidfighting 2008-04-07
  • 打赏
  • 举报
回复
up
qmm161 2008-04-07
  • 打赏
  • 举报
回复
在值型类中如果涉及到指针成员就需要用到深拷贝,如果是指针型类,如智能指针类,则另当别论!
  • 打赏
  • 举报
回复
楼上的题没审清!
Alix-Lei 2008-04-07
  • 打赏
  • 举报
回复
ding
lily604 2008-04-07
  • 打赏
  • 举报
回复
引用33楼.............
领悟了概念还会看不懂代码。。。。?
..................
systemthink 2008-04-07
  • 打赏
  • 举报
回复
MARK~
babyvox1999 2008-04-07
  • 打赏
  • 举报
回复
领悟了概念还会看不懂代码。。。。?
帅得不敢出门 2008-04-07
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 coding_hello 的回复:]
牢记一点,来成员中有指针,多半都需要深拷贝。因为涉及到动态分配的资源,不能简单的共享。
[/Quote]
up
aayzaayz 2008-04-07
  • 打赏
  • 举报
回复
yun
舉杯邀明月 2008-04-07
  • 打赏
  • 举报
回复
学习...........

JF.........
pigeggs 2008-04-07
  • 打赏
  • 举报
回复
一个概念,怎么写这么长代码,大哥的专业精神,值得佩服
jieao111 2008-04-06
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 ttkk_2007 的回复:]
可学会了
深拷贝就是不要让两个指针指向同一位置,否则释放的时候会有问题
points=new point[numberofpoints];这一句并没有写成points = pointsarray.points;就是为了不要让两个指针指向同一地址
[/Quote]
野男孩 2008-04-06
  • 打赏
  • 举报
回复
牢记一点,来成员中有指针,多半都需要深拷贝。因为涉及到动态分配的资源,不能简单的共享。
ttkk_2007 2008-04-06
  • 打赏
  • 举报
回复
可学会了
深拷贝就是不要让两个指针指向同一位置,否则释放的时候会有问题
points=new point[numberofpoints];这一句并没有写成points = pointsarray.points;就是为了不要让两个指针指向同一地址
加载更多回复(17)

64,849

社区成员

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

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