1. 程式人生 > >《C++語言程式設計基礎》學習之物件複製與移動

《C++語言程式設計基礎》學習之物件複製與移動

淺層複製與深層複製 淺層複製,實現物件間資料元素的一一對應複製。 深層複製,當被複制的物件資料成員是指標型別時,不是複製該指標成員本身,而是將指標所指物件進行復制。 物件的淺層複製:傳遞的是指標的值

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;
	}
	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 points(count);//建立陣列物件
	//points.element(0)呼叫類函式,返回物件陣列中的元素的引用,訪問陣列元素的成員
	points.element(0).move(5, 0);
	points.element(1).move(15, 20);//訪問陣列元素的成員

	ArrayOfPoints pointsArrayt(points);//建立副本指標指向原先的陣列
	cout << "Copy of points:" << endl;
	cout << "Point_0 of array2: " << pointsArrayt.element(0).getX() << ", "
		<< pointsArrayt.element(0).getY() << endl;
	cout << "Point_1 of array2: " << pointsArrayt.element(1).getX() << ", "
		<< pointsArrayt.element(1).getY() << endl;
	points.element(0).move(25, 30);
	points.element(1).move(35, 40);
	cout << "After the moving of points:" << endl;
	cout << "Point_0 of array2: " << pointsArrayt.element(0).getX() << ", "
		<< pointsArrayt.element(0).getY() << endl;
	cout << "Point_1 of array2: " << pointsArrayt.element(1).getX() << ", "
		<< pointsArrayt.element(1).getY() << endl;
	return 0;
}

物件的深層複製: 在類ArrayOfPoints中增加複製建構函式

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;
	}
	Point& element(int index) {   //返回陣列元素的引用
		assert(index >= 0 && index < size);
		return points[index];
	}
	ArrayOfPoints(const ArrayOfPoints& pointsArray);
private:
	Point *points;//指向動態陣列首地址
	int size; //陣列大小
};
ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints& v) { //複製建構函式
	cout << "Copy function done!";
	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 points(count);//建立陣列物件
	//points.element(0)呼叫類函式,返回物件陣列中的元素的引用,訪問陣列元素的成員
	points.element(0).move(5, 0);
	points.element(1).move(15, 20);//訪問陣列元素的成員

	ArrayOfPoints pointsArrayt(points);//建立副本指標指向原先的陣列
	cout << "Copy of points:" << endl;
	cout << "Point_0 of array2: " << pointsArrayt.element(0).getX() << ", "
		<< pointsArrayt.element(0).getY() << endl;
	cout << "Point_1 of array2: " << pointsArrayt.element(1).getX() << ", "
		<< pointsArrayt.element(1).getY() << endl;
	points.element(0).move(25, 30);
	points.element(1).move(35, 40);
	cout << "After the moving of points:" << endl;
	cout << "Point_0 of array2: " << pointsArrayt.element(0).getX() << ", "
		<< pointsArrayt.element(0).getY() << endl;
	cout << "Point_1 of array2: " << pointsArrayt.element(1).getX() << ", "
		<< pointsArrayt.element(1).getY() << endl;
	return 0;
}