要实现深拷贝就需要自己编写拷贝构造函数。

深拷贝

#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 x,int y)

                   {  X=x;  Y=y;   }

  private:

       int  X,Y;

};

class ArrayOfPoints

{   public:

     ArrayOfPoints(ArrayOfPoints& pointsArray);

     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;

};

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;

} //程序的运行结果如下:

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.

C++——深拷贝的更多相关文章

  1. python之浅拷贝和深拷贝

    1.浅拷贝 1>赋值:从下面的例子我们可以看到赋值之后新变量的内存地址并没有发生任何变化,实际上python中的赋值操作不会开辟新的内存空间,它只是复制了新对象的引用,也就是说除了b这个名字以外 ...

  2. python的拷贝(深拷贝和浅拷贝)

    今天看了几篇关于python拷贝的博文,感觉不太清楚,所以我就自己做实验试一下,特此记录. 拷贝是针对组合对象说的,比如列表,类等,而数字,字符串这样的变量是没有拷贝这一说的. 实现拷贝有: 1.工厂 ...

  3. C# 深拷贝的具体代码的封装与调用

    先封装下实现方法: public class DeepClone { public static object CopyObject(Object obj) { if (obj == null) { ...

  4. C#设计模式:原型模式(Prototype)及深拷贝、浅拷贝

    原型模式(Prototype) 定义: 原型模式:用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象.被复制的实例被称为原型,这个原型是可定制的. Prototype Pattern也是一 ...

  5. Objective-C中的浅拷贝和深拷贝(转载)

    本文转自:http://segmentfault.com/blog/channe/1190000000604331 浅拷贝 浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间.如: ...

  6. Java对象的深拷贝和浅拷贝、集合的交集并集

    http://blog.csdn.net/lian_1988/article/details/45970927 http://www.cnblogs.com/yxnchinahlj/archive/2 ...

  7. C++ 系列:深拷贝与浅拷贝

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  8. [转] js对象浅拷贝和深拷贝详解

    本文为大家分享了JavaScript对象的浅拷贝和深拷贝代码,供大家参考,具体内容如下 1.浅拷贝 拷贝就是把父对像的属性,全部拷贝给子对象. 下面这个函数,就是在做拷贝: var Chinese = ...

  9. Objective-C中的深拷贝和浅拷贝

    在Objective-C中对象之间的拷贝分为浅拷贝和深拷贝.说白了,对非容器类的浅拷贝就是拷贝对象的地址,对象里面存的内容仍然是一份,没有新的内存被分配.对非容器类的深拷贝就是重写分配一块内存,然后把 ...

  10. $.extend()的深拷贝和浅拷贝详细讲解

    版权声明:作者原创,转载请注明出处! 语法:jQuery.extend( [deep ], target, object1 [, objectN ] ) 描述: 将两个或更多对象的内容合并到第一个对象 ...

随机推荐

  1. html5之table嵌入form表单布局(务必注意:table标签必须在form表单内部,不能再form表单外部!)

    切记:用table标签来布局form表单元素,table标签必须放在form表单内部,否则可能会出现各种bug 原文地址:https://blog.csdn.net/weixin_43343144/a ...

  2. Linux学习Day6:编写Shell脚本

    Shell脚本命令的工作方式有两种: 交互式(Interactive):用户每输入一条命令就立即执行. 批处理(Batch):由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中诸多 ...

  3. Linux 简介、目录结构

    Linux是类 Unix 操作系统. 根据原生程度可分为: 内核版本 发行版本:一些公司.组织在内核版的基础上进行二次开发 根据市场需求可分为: 服务器版:没有好看的界面,在终端操作,类似于dos 桌 ...

  4. 服务端性能测试工具校验v1.2

    服务端性能测试工具校验v1.2 想知道压力工具实际并发多少,想知道压力工具统计响应数据准不准,来试试这款校准工具. 更新说明: 1.修正总接收请求显示上限. 2.随着响应时间增加,自动增加处理线程. ...

  5. 解决在jsp页面中使用jstl无法遍历request域中list集合的问题

    解决在jsp页面中使用jstl无法遍历request域中list集合的问题 1. 前言 ​ 最近在写一个很简单的Javaweb项目,里面需要将request域中的list集合加载到jsp页面,我使用e ...

  6. oracle数据库应用开发经验

    l  日志表应该以时间做分区,方便清理 一般应用都会有一些表用来记录用户操作日志,数据变更记录,交易流水等日志型的库表.这些表最好按时间字段做分区,这样在迁移或者清理历史记录时会比较方便,借助orac ...

  7. 有关使用phpstudy搭建sqli-lab环境搭建时发生Uncaught Error: Call to undefined function mysql_connect()错误

    文章更新于2020-1-30 问题描述 Uncaught Error: Call to undefined function mysql_connect() 分析 经查php手册可知 mysql_co ...

  8. eclipse 项目资源与 java 编译器级别不致总是;说明资源路径位置类型Java编译器级别不匹配t

    问题:Description Resource Path Location Type Java compiler level does not match t 今天在自己项目中整合HBase API的 ...

  9. [Python机器学习]鸢尾花分类 机器学习应用

    1.问题简述 假设有一名植物学爱好者对她发现的鸢尾花的品种很感兴趣.她收集了每朵鸢尾花的一些测量数据: 花瓣的长度和宽度以及花萼的长度和宽度,所有测量结果的单位都是厘米. 她还有一些鸢尾花的测量数据, ...

  10. H5---禁止长按

    以上行为可以总结成这几个(每个手机以及浏览器的表现形式不一样):长按图片保存.长按选择文字.长按链接/手机号/邮箱时呼出菜单. 想要禁止这些浏览器的默认行为,可以使用以下CSS: // 禁止长按图片保 ...