首页 > 其他分享 >郑莉cpp例6-22 浅层复制与深层复制

郑莉cpp例6-22 浅层复制与深层复制

时间:2024-03-10 17:24:51浏览次数:22  
标签:22 Point ArrayOfPoints points 复制 cpp called size

浅层复制与深层复制

浅层复制并没有形成真正的副本,存在两个对象共用同一块内存空间而导致执行析构函数时,该空间被两次释放,导致运行错误。

深层复制则实现,复制之后,两个对象不互相影响。

#include <iostream>
using namespace std;
#include <cassert>

class Point{
public:
    Point():x(0),y(0)   {cout << "Default Constructor called." << endl;}
    Point(int x,int y):x(x),y(y)    {cout << "Constructor called." << endl;}
    ~Point()    {cout << "Destructor called." << endl;}
    int getX() const    {return x;}
    int getY() const    {return y;}
    void move(int newX,int newY)    {x = newX;y = newY;}
private:
    int x,y;
};

class ArrayOfPoints{
public:
    ArrayOfPoints(int size):size(size)    {points = new Point[size];}
    ~ArrayOfPoints() {
        cout << "Deleting..." << endl;
        delete[] points;
    }

    Point &element(int index) {
        assert(index >= 0 && index < size);
        return points[index];
    }

    ArrayOfPoints(const ArrayOfPoints &v);

private:
    Point *points;
    int size;
};

ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints &v)
{
    size = v.size;
    points = new Point[size];
    for(int i = 0;i < size;i++)
    {
        points[i] = v.points[i];
    }
}

int main()
{
    int count;
    cout << "Please enter the count of points:";
    cin >> count;
    ArrayOfPoints pointsArray1(count);
    pointsArray1.element(0).move(5,0);
    pointsArray1.element(1).move(15,20);

    cout << "-------------------------" << endl;

    ArrayOfPoints pointsArray2 = pointsArray1;
    cout << "Copy of pointsArrayl : " << endl;
    cout << "Point_0 of array2 : " << pointsArray2.element(0).getX() << " , " << pointsArray2.element(0).getY() << endl;
    cout << "Point_l of array2 : " << pointsArray2.element(1).getX() << " , " << pointsArray2.element(1).getY() << endl;

    cout << "-------------------------" << endl;

    pointsArray1.element(0).move(25,30);
    pointsArray1.element(1).move(35,40);
    cout << "After the moving of pointsArrayl : " << 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;

    return 0;
}

结果

Please enter the count of points:2
Default Constructor called.
Default Constructor called.
-------------------------
Default Constructor called.
Default Constructor called.
Copy of pointsArrayl :
Point_0 of array2 : 5 , 0
Point_l of array2 : 15 , 20
-------------------------
After the moving of pointsArrayl :
Point_0 of array2 : 5 , 0
Point_1 of array2 : 15 , 20
Deleting...
Destructor called.
Destructor called.
Deleting...
Destructor called.
Destructor called.

 

标签:22,Point,ArrayOfPoints,points,复制,cpp,called,size
From: https://www.cnblogs.com/uacs2024/p/18064420

相关文章

  • Redis 架构深入:主从复制、哨兵到集群
    大家好,我是小康,今天我们来聊下Redis的几种架构模式,包括主从复制、哨兵和集群模式。前言:设想一下,你的咖啡馆在城市中太受欢迎,导致每天都人满为患。为了缓解这种压力,你决定在其他地方开设分店,这样顾客就可以在附近的分店享受咖啡,而不必涌向一个地方,这就好比Redis的主从复制,让......
  • MYSQL学习笔记22: 多表查询
    多表查询单表查询查询emp表select*fromemp;查询dept表select*fromdept;笛卡尔积(全组合)#emp表有4条记录,dept表有6条记录#笛卡尔积有4*6=24条记录select*fromemp,dept;消除无效的笛卡尔积(emp和dept通过dept_id连接)select*fromemp,deptw......
  • 代码随想录 第十六天 | ● 104.二叉树的最大深度 559.n叉树的最大深度 ● 111.二叉树
    leetcode:104.二叉树的最大深度-力扣(LeetCode)思路:递归判断每次左右节点的是否存在,存在自然加一,return的1就是这样,判断子节点的左右两端是否有节点,统计有的节点数量,也就是左右的高度classSolution{publicintmaxDepth(TreeNoderoot){//后序遍历if......
  • ubuntu22.04编译创龙T113-i mini的SDK
    ubuntu版本22.04.11.解压安装包拷贝sdk并解压出来,注意安装包较大请预留好硬盘空间2.预安装编译应用先安装如下应用,在编译过程中需要使用到的依赖sudoaptinstallbuild-essentialcmakeflexbisonu-boot-toolsopenssllibssl-devtexinfo3.安装和更换python2编译使......
  • 代码随想录 第十五天 | ● 层序遍历 10 ● 226.翻转二叉树 ● 101.对称二叉树 2 感
    leetcode:102.二叉树的层序遍历-力扣(LeetCode)思路:用队列长度控制弹栈的多少,不等于空时获取root,因为传了一个根肯定是1,接下来找左右节点,将根节点弹出,获取下一次的size,一直到空。。。//102.二叉树的层序遍历classSolution{publicList<List<Integer>>resList=newA......
  • 使用 Visual Studio 2022 直接调试 WebAPI
    参考资料https://learn.microsoft.com/zh-cn/aspnet/core/test/http-files?view=aspnetcore-8.0在没有Postman等专门软件环境下,有没有轻量的调试http方法呢?尤其是每天都要打开宇宙第一IDE的环境,其实VS本身就带了一种方式,就是创建一个http文件来完成这个工作.VisualStud......
  • macOS Ventura 13.6.5 (22G621) 正式版发布,ISO、IPSW、PKG 下载 (安全更新)
    macOSVentura13.6.5(22G621)正式版发布,ISO、IPSW、PKG下载(安全更新)3月8日凌晨,macOSSonoma14.4发布,同时带来了macOSVentru13.6.5和macOSMonterey12.7.4安全更新。macOSVentura13.6及更新版本,如无特殊说明皆为安全更新,不再赘述。请访问原文链接:https://......
  • macOS Ventura 13.6.5 (22G621) Boot ISO 原版可引导镜像下载
    macOSVentura13.6.5(22G621)BootISO原版可引导镜像下载3月8日凌晨,macOSSonoma14.4发布,同时带来了macOSVentru13.6.5和macOSMonterey12.7.4安全更新。macOSVentura13.6及更新版本,如无特殊说明皆为安全更新,不再赘述。本站下载的macOS软件包,既可以拖拽到......
  • ubuntu 22.04 安装samba服务
    1.安装软件sudoaptinstallsambasamba-common如果出现类似错误:dpkg:处理软件包samba-common-bin(--configure)时出错参考如下处理:sudosumv/var/lib/dpkg/info/var/lib/dpkg/info_bakmkdir/var/lib/dpkg/infoapt-getupdate&&apt-get-finstallmv/var/l......
  • mongodb6.0.13 搭建复制集PSA
    一、材料mongodb:6.0.13mongosh:2.1.5  openssl-1.1.1w系统:redhat4.8二、模式PDA:一个主节点(Primary)、  一个延迟副节点(SEcondary)、一个仲裁节点(arbiter)PDA主节点延迟副节点仲裁节点名称PrimarySecondaryArbiter端口27017......