C++ 类的聚集和浅拷贝与深拷贝
来源:互联网 发布:一维数组的表达方式 编辑:程序博客网 时间:2024/06/01 08:31
该类的对象的数据将存放在对象外边,对象中只存放数据的地址。数据可以是数组、对象等。
2.浅拷贝与深拷贝
浅拷贝
–实现对象间数据元素的一一对应复制。这是拷贝构造函数的本能。当数据元素是指针的时候,则出问题。
深拷贝
–当被复制的对象数据成员是指针类型时,不是复制该指针成员本身,而是将指针所指的对象(区域)进行复制。
对象的浅拷贝
//对象的浅拷贝#include<iostream>using namespace std;class Point{ public:Point(){ X=Y=0; cout<<"Default Constructor called.\n";}Point(int xx,int yy){ X=xx; Y=yy; cout<< "Constructor called.\n"; }~Point() { cout<<"Destructor called.\n"; }int GetX() {return X;}int GetY() {return Y;}void Move(int x,int y){ X=x; Y=y; }private:int X,Y;};class ArrayOfPoints //这个类叫“控制类”{ public:ArrayOfPoints(int n) // 按照指示创建指定个数的对象{ numberOfPoints = n;points = new Point[n]; }~ArrayOfPoints(){ cout<<"Deleting..."<<endl;numberOfPoints=0; delete[] points; }Point& Element(int n){ return points[n]; }private:Point *points; //类内只保存对象数组的首址int numberOfPoints; //对象的个数};void 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;//对象1的值改变同时也会改变对象2的值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;}运行结果:
Please enter the number of points:2
Default Constructor called.
Default Constructor called.
Copy of pointsArray1:
Point_0 of array2: 5, 10
Point_1 of array2: 15, 20
After the moving of pointsArray1:
Point_0 of array2: 25, 30
Point_1 of array2: 35, 40
Deleting...
Destructor called.
Destructor called.
Deleting...(同一块空间被释放了两次)
接下来程序出现异常,也就是运行错误。
对象的深拷贝
//对象的深拷贝#include<iostream>using namespace std;class Point{ public:Point(){ X=Y=0; cout<<"Default Constructor called.\n";}Point(int xx,int yy){ X=xx; Y=yy; cout<< "Constructor called.\n"; }~Point() { cout<<"Destructor called.\n"; }int GetX() {return X;}int GetY() {return Y;}void Move(int x,int y){ X=x; Y=y; }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());}void 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);cout << "test\n";//对象的深拷贝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; //深拷贝对象2的值不会因对象1的值改变而改变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;}运行结果:
Please enter the number of points:2
Default Constructor called.
Default Constructor called.
Default Constructor called.
Default Constructor called.
Copy of pointsArray1:
Point_0 of array2: 5, 10
Point_1 of array2: 15, 20
After the moving of pointsArray1:
Point_0 of array2: 5, 10
Point_1 of array2: 15, 20
Deleting...
Destructor called.
Destructor called.
Deleting...
Destructor called.
Destructor called.
Press any key to continue
使用动态存储分配的类要重写拷贝构造函数!
- C++ 类的聚集和浅拷贝与深拷贝
- C++ 类的聚集和浅拷贝与深拷贝
- C++ 类的聚集和浅拷贝与深拷贝
- c++中 拷贝构造函数的深拷贝和浅拷贝--“浅拷贝”与“深拷贝”
- 类的深拷贝与浅拷贝
- object c的浅拷贝(地址拷贝)和深拷贝(对象拷贝)
- c++中类的位拷贝与拷贝构造函数(浅拷贝与深拷贝)
- c++-深拷贝与浅拷贝
- 【c++】浅拷贝与深拷贝
- Objective-C 深拷贝与浅拷贝
- [C++]深拷贝与浅拷贝
- 【C++】对深拷贝与浅拷贝的再次理解
- C++——string的深拷贝与浅拷贝
- Object-c 深拷贝和浅拷贝
- [Objective-C]浅拷贝和深拷贝
- 【C++】深拷贝和浅拷贝
- 【C++】深拷贝和浅拷贝
- Objective-C浅拷贝和深拷贝
- strcpy和strncpy
- WebLogic修改页面后不刷新显示
- 成为PHP编程高手应该遵循的三个准则
- HttpServlet 接口及父类的对应关系图
- 兼容不同版本浏览器及版本号来创建XMLHttpRequest对象
- C++ 类的聚集和浅拷贝与深拷贝
- zoj_1184
- 《30天精通iPhone手机编程》-Day20-DJ混音器
- Const指针与Const常量的在C编译器之间的匹配
- android 分享带图片的信息到彩信,微薄====
- 搭建VS2005 Wince开发环境
- poj 1969 Count on Canton
- hdu 1536 (S-Nim) SG解法
- epoll 的 EPOLLET 和 EPOLLLT