copy构造函数使用深copy

本文深入探讨了C++中深拷贝构造函数的实现原理,通过具体代码示例展示了如何为自定义类编写深拷贝构造函数,以避免浅拷贝带来的问题。文章详细解释了在对象初始化和赋值过程中深拷贝构造函数的调用时机。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

=操作符的默认copy构造函数是浅copy,要是想使用深copy需要编写copy构造函数,编写深copy构造函数的形式如下,调用方式除了显示的调用,当首次定义对象,并使用=进行对象初始化的时候也会调用该copy构造函数 Array a2 = a1;

Array::Array(const Array& obj)
{
  this->m_length = obj.m_length;
  this->m_space = new int[this->m_length]; //分配内存空间

  for (int i=0; i<m_length; i++) //数组元素复制
  {
  	this->m_space[i] = obj.m_space[i];
  }
}

#include "myarray.h"

//

//int m_length;
//char *m_space;
Array::Array(int length)
{
	if (length < 0)
	{
		length = 0; //
	}

	m_length = length;
	m_space = new int[m_length];
}

//Array a2 = a1;
// 当首次定义对象并调用 = 操作符的时候,调用该深copy构造函数,其他时候的 =就是=操作符
Array::Array(const Array& obj)
{
	this->m_length = obj.m_length;
	this->m_space = new int[this->m_length]; //分配内存空间

	for (int i=0; i<m_length; i++) //数组元素复制
	{
		this->m_space[i] = obj.m_space[i];
	}
}
Array::~Array()
{
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;
		m_length = -1;
	}
}

//a1.setData(i, i);
void Array::setData(int index, int valude)
{
	m_space[index] = valude;
}
int Array::getData(int index)
{
	return m_space[index];
}
int Array::length()
{
	return m_length;
}


test.cpp函数

#include <iostream>

using namespace std;


class TestCopy
{
private:
    /* data */
    int legth;
    int *pArray;
public:
    TestCopy(int len);
    ~TestCopy();
    // TestCopy t2 = t1;  特殊构造函数   当首次定义t2并调用=的时候,就会调用该构造函数
    TestCopy(TestCopy & test);
    int setArray(int index, int valued);
    int getLength(void);
    int printArray(int index);
};

TestCopy::TestCopy(int len)
{
    if(len < 0)
    {
        len = 0;
    }

    legth = len;
    pArray = new int[len];

}

TestCopy::~TestCopy()
{
        if(pArray != NULL)
        {
            delete[] pArray;
            legth = -1;
        }
}

TestCopy::TestCopy(TestCopy & test)
{
    this->legth = test.legth;
    this->pArray = new int[this->legth];

    for (int i = 0; i < this->legth; i++)
    {
        this->pArray[i] = test.pArray[i];
    }
    cout << "construct function is called"  << endl;
}


int TestCopy::setArray(int index, int valued)
{
    this->pArray[index] = valued;
    return 0;
}


int TestCopy::getLength(void)
{
    return this->legth;
}


int TestCopy::printArray(int index)
{
    cout << this->pArray[index] << endl;
    return 0;
}

int main(int argc, const char** argv) 
{    
    
    TestCopy t1(10);
    
    for(int i = 0; i < t1.getLength(); i ++)
    {
        t1.setArray(i, i);
    }

    // 开始调用构造函数
    cout << "---------------------start-----------------" << endl;

    TestCopy t2 = t1;

    cout << "----------------------end------------------" << endl;
    for(int i = 0; i  < t2.getLength(); i++)
    {
        t2.printArray(i);
    }

    
    
    
    return 0;
}




输出结果:

$ ./a.out 
---------------------start-----------------
construct function is called
----------------------end------------------
0
1
2
3
4
5
6
7
8
9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Achilles.Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值