c指针的浅层复制问题_C++浅层复制与深层复制(学习笔记:第6章 20)

浅层复制与深层复制[1]

  • 浅层复制
    • 实现对象间数据元素的一一对应复制。
  • 深层复制
    • 当被复制的对象数据成员是指针类型时,不是复制该指针成员本身,而是将指针所指对象进行复制。

例6-21 对象的浅层复制

#include <iostream>
#include <cassert>
using namespace std;
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];
	}
private:
	Point *points; //指向动态数组首地址
	int size; //数组大小
};

int main() {
	int count;
	cout << "Please enter the count of points: ";
	cin >> count;
	ArrayOfPoints pointsArray1(count); //创建对象数组
	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;
	return 0;
}

运行结果如下:

2271c3172014069479da77ddea487ef1.png

程序出现运行错误。

原因分析:

c2a56bc37d79cb870278171218b996eb.png
pointsArray1内部指针指向一个数组,占有两个元素的内存单元。pointsArray2复制pointsArray1的时候,只是把指针里面的内容原样搬到pointsArray2的指针里,size的值原样搬到pointsArray2的size里,没做其他的事情,就只是一一对应复制。这样就导致了两个指针存的都是同一个地址,即两个指针指向同一个数组内存单元。数组根本没有被复制,只是把它的地址复制了。所以析构的时候,在析构pointsArray1的时候已经把这数组的内存单元释放了,析构pointsArray2的时候,又delete一遍,所以在这个地方就出错了。

例6-22 对象的深层复制

#include <iostream>
#include <cassert>
using namespace std;
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(const ArrayOfPoints& pointsArray);//复制构造函数
	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];
	}
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, 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;
	return 0;
}

程序的运行结果如下:

e23f52f6d7f9972972ce6d597aebb3a0.png

这个例子中自己写了一个复制构造函数,可以实现深层复制,默认的复制构造函数实现的是浅层复制,不符和要求,会出错。

用图来表示自己写的复制构造函数的深层复制的过程,如下图所示:

e8b19633aafffdf6072923ab0c6ae0ed.png

参考

  1. ^http://www.xuetangx.com/courses/course-v1:TsinghuaX+00740043X_2015_T2+sp/courseware/d4eb7d174ba04a4da6282bcae197892c/328f49c8f22b4e8cb8fd3febf2ac8868/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值