动态数组类/对象的浅复制/对象深复制

本文通过封装动态数组为类的方式介绍了C++中对象的浅复制与深复制概念及其实现方法。演示了如何避免浅复制导致的问题,并展示了深复制如何确保两个对象互不影响。

1.将动态数组封装成类

#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 << "Construct 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) ;
	~ArrayOfPoints();
	Point& element(int index);

private:
	Point *points;  //指向动态数组首地址
	int size;  //数组大小

};

ArrayOfPoints::ArrayOfPoints(int _size)
{
	size = _size;
	points = new Point[size];
}

ArrayOfPoints::~ArrayOfPoints()
{
	cout << "Deleting ..." << endl;
	delete[] points;
}

Point& ArrayOfPoints::element(int index)
{
	assert(index >= 0 && index < size);
	return points[index];
}
int main()
{
	int count;
	cout << "Please enter the count of points:";
	cin >> count;
	ArrayOfPoints points(count);  //创建数组对象
	points.element(0).move(5, 0);  //访问数组元素的成员
	points.element(1).move(15, 20);
	return 0;
}


2.对象浅复制

#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 << "Construct 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) ;
	~ArrayOfPoints();
	Point& element(int index);

private:
	Point *points;  //指向动态数组首地址
	int size;  //数组大小

};

ArrayOfPoints::ArrayOfPoints(int _size)
{
	size = _size;
	points = new Point[size];
}

ArrayOfPoints::~ArrayOfPoints()
{
	cout << "Deleting ..." << endl;
	delete[] points;
}

Point& ArrayOfPoints::element(int index)
{
	assert(index >= 0 && index < size);
	return points[index];
}
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);

	ArrayOfPoints pointsArray2(pointsArray1);  //调用默认的复制构造函数
	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;
}
对象1的改变引起对象2(浅复制得到)的改变。


3.对象的深复制

#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 << "Construct 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) ;
	~ArrayOfPoints();
	Point& element(int index);
	ArrayOfPoints(const ArrayOfPoints& pointArray);

private:
	Point *points;  //指向动态数组首地址
	int size;  //数组大小

};

ArrayOfPoints::ArrayOfPoints(int _size)
{
	size = _size;
	points = new Point[size];
}

ArrayOfPoints::~ArrayOfPoints()
{
	cout << "Deleting ..." << endl;
	delete[] points;
}

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

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);

	ArrayOfPoints pointsArray2(pointsArray1);  //调用默认的复制构造函数
	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;
}

对象1的改变不引起对象2的改变


来自清华MOOC课件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值