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

深拷贝

#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. ARC-082F Sandglass

    题意 有一个含有两个玻璃球的沙漏,分别称这两个玻璃球为\(

  2. Ajax0002: 省市县三级联动案例

  3. Vue之计算属性Computed和属性监听Watch,Computed和Watch的区别

    一. 计算属性(computed) 1.计算属性是为了模板中的表达式简洁,易维护,符合用于简单运算的设计初衷. 例如: <div id="app"> {{ myname ...

  4. 【Java】实验代码整理(多线程、自定义异常、界面)

    1.界面+文件输入输出流 package finalExam; import java.awt.BorderLayout; import java.awt.Container; import java ...

  5. Python基础之程序暂停

    当我们执行某些程序时,由于机器速度很快导致肉眼无法直接看到执行结果时程序便停止运行.这时候我们迫切需要在程序中暂停,专业术语叫做阻塞.下面列举几种常用的程序暂停方法: input()用法:直接在欲等待 ...

  6. 通过phpstorm管理svn的gui界面报错问题

    mac通过phpstorm来gui管理svn时,提示Invalid VCS root mappings 原因是因为mac中的svn版本过高,phpstorm可以兼容的版本是svn 1.6 \ 1.7 ...

  7. 利用 serviceStack 搭建web服务器

    1,资料地址 参考资料 https://docs.servicestack.net/ https://docs.servicestack.net/create-your-first-webservic ...

  8. 分享10个免费或便宜的Photoshop替代工具

    说到编辑照片和图像文件,一般很多人都使用photoshop软件.然而,使用现在的最新版本Photoshop CC每月最低也要支付980日元,感觉使用门槛有点高的人应该不少吧. 有一篇文章,推荐了10个 ...

  9. 在VS的依赖项中引用项目

    操作步骤:鼠标右击项目(注意是项目)->添加->引用->项目(在项目列表中选择需要引用的项目)->确定

  10. WSL的ssh-agent问题

    WSL , 使用forwardAgent 的时候 , 用的以下两个应用 ubuntu 18.04 . ubuntu 这两个应用的 ssh-agent是有问题的 . 详见 https://github. ...