# 为深拷贝散分!

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

...全文
242 37 打赏 收藏 转发到动态 举报

37 条回复

hui12345685 2008-04-07
• 打赏
• 举报

jf
• 打赏
• 举报

• 打赏
• 举报

jf
qinqinhao 2008-04-07
• 打赏
• 举报

xbt746 2008-04-07
• 打赏
• 举报

DL88250 2008-04-07
• 打赏
• 举报

paidfighting 2008-04-07
• 打赏
• 举报

up
qmm161 2008-04-07
• 打赏
• 举报

• 打赏
• 举报

Alix-Lei 2008-04-07
• 打赏
• 举报

ding
lily604 2008-04-07
• 打赏
• 举报

．．．．．．．．．．．．．．．．．．
systemthink 2008-04-07
• 打赏
• 举报

MARK~
babyvox1999 2008-04-07
• 打赏
• 举报

• 打赏
• 举报

[Quote=引用 19 楼 coding_hello 的回复:]

[/Quote]
up
aayzaayz 2008-04-07
• 打赏
• 举报

yun

• 打赏
• 举报

JF.........
pigeggs 2008-04-07
• 打赏
• 举报

jieao111 2008-04-06
• 打赏
• 举报

[Quote=引用 18 楼 ttkk_2007 的回复:]

points=new point[numberofpoints];这一句并没有写成points = pointsarray.points;就是为了不要让两个指针指向同一地址
[/Quote]

• 打赏
• 举报

ttkk_2007 2008-04-06
• 打赏
• 举报

points=new point[numberofpoints];这一句并没有写成points = pointsarray.points;就是为了不要让两个指针指向同一地址

64,746

c++ 技术论坛（原bbs）

• 近7日
• 近30日
• 至今

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下