注意:本文中代碼均使用 Qt 開(kāi)發(fā)編譯環(huán)境
//“淺拷貝”舉例:
#include <QCoreApplication>
#include <QDebug>
class Point
{
public:
Point(){
X=Y=0;
qDebug() << "Default Constructor called.";
}
Point(int xx,int yy){
X=xx;
Y=yy;
qDebug() << "Constructor called.";
}
~Point(){
qDebug() << "Destructor called.";
}
int GetX() const {
return X;
}
int GetY() const {
return Y;
}
void Move(int x,int y){
X=x;
Y=y;
}
private:
int X,Y;
};
class ArrayOfPoints
{
public:
ArrayOfPoints(int n){
numberOfPoints = n;
points = new Point[n];
}
~ArrayOfPoints(){
qDebug() << "Deleting...";
numberOfPoints = 0;
delete [] points;
}
Point& Element(int n){
return points[n];
}
private:
Point *points;
int numberOfPoints;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int number = 64;
ArrayOfPoints pointArray1(number); // 創(chuàng)建對(duì)象數(shù)組
pointArray1.Element(0).Move(5,10); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
pointArray1.Element(1).Move(15,20); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
ArrayOfPoints pointArray2(pointArray1); // 創(chuàng)建對(duì)象數(shù)組副本
qDebug() << "Copy of pointsArray1: ";
qDebug() << "Point_0 of array2: " << pointArray2.Element(0).GetX() << "," << pointArray2.Element(0).GetY();
qDebug() << "Point_1 of array2: " << pointArray2.Element(1).GetX() << "," << pointArray2.Element(1).GetY();
pointArray1.Element(0).Move(25,30); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
pointArray1.Element(1).Move(35,40); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
qDebug() << "After the moving of pointArray1: ";
qDebug() << "Point_0 of array2: " << pointArray2.Element(0).GetX() << "," << pointArray2.Element(0).GetY();
qDebug() << "Point_1 of array2: " << pointArray2.Element(1).GetX() << "," << pointArray2.Element(1).GetY();
return a.exec();
}
//運(yùn)行結(jié)果:
淺拷貝運(yùn)行結(jié)果
//拷貝前后示意圖:
拷貝前后示意圖
從圖中可以看出默認(rèn)的拷貝構(gòu)造函數(shù)將兩個(gè)對(duì)象的對(duì)應(yīng)數(shù)據(jù)項(xiàng)簡(jiǎn)單復(fù)制后掰吕,pointArray1 的成員 points 和 pointArray2 的成員 points 具有相同的值扣孟,也就是說(shuō)兩個(gè)指針指向同一個(gè)內(nèi)存地址,表面上好像完成了復(fù)制柄瑰,但是并沒(méi)有形成真正的副本。因此當(dāng)程序中移動(dòng) pointArray1 中的點(diǎn)時(shí)快毛,也影響到了 pointArray2 晤斩。這種效果就是淺拷貝。
淺拷貝還有更大的弊病宴霸,在程序結(jié)束之前 pointArray1 和 pointArray2 的析構(gòu)函數(shù)會(huì)被自動(dòng)調(diào)用囱晴,動(dòng)態(tài)分配的內(nèi)存空間會(huì)被釋放。由于兩個(gè)對(duì)象共用了同一塊內(nèi)存空間瓢谢,因此該空間被釋放兩次畸写,于是導(dǎo)致運(yùn)行時(shí)錯(cuò)誤。解決辦法是編寫(xiě)拷貝構(gòu)造函數(shù)氓扛,實(shí)現(xiàn)“深拷貝”枯芬。
下面是對(duì)上述代碼進(jìn)行調(diào)整,加入拷貝構(gòu)造函數(shù)之后采郎,用于實(shí)現(xiàn)“深拷貝”的代碼:
#include <QCoreApplication>
#include <QDebug>
class Point
{
public:
Point(){
X=Y=0;
qDebug() << "Default Constructor called.";
}
Point(int xx,int yy){
X=xx;
Y=yy;
qDebug() << "Constructor called.";
}
~Point(){
qDebug() << "Destructor called.";
}
int GetX() const {
return X;
}
int GetY() const {
return Y;
}
void Move(int x,int y){
X=x;
Y=y;
}
private:
int X,Y;
};
class ArrayOfPoints
{
public:
ArrayOfPoints(int n){
numberOfPoints = n;
points = new Point[n];
}
ArrayOfPoints(ArrayOfPoints& pointsArray); ///< 實(shí)現(xiàn)深拷貝
~ArrayOfPoints(){
qDebug() << "Deleting...";
numberOfPoints = 0;
delete [] points;
}
Point& Element(int n){
return points[n];
}
private:
Point *points;
int numberOfPoints;
};
ArrayOfPoints::ArrayOfPoints(ArrayOfPoints& pointsArray){ ///< 實(shí)現(xiàn)深拷貝
numberOfPoints = pointsArray.numberOfPoints;
points = new Point[numberOfPoints];
for (int i=0; i<numberOfPoints; i++) {
points[i].Move(pointsArray.Element(i).GetX(), pointsArray.Element(i).GetY());
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int number = 64;
ArrayOfPoints pointArray1(number); // 創(chuàng)建對(duì)象數(shù)組
pointArray1.Element(0).Move(5,10); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
pointArray1.Element(1).Move(15,20); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
ArrayOfPoints pointArray2(pointArray1); // 創(chuàng)建對(duì)象數(shù)組副本
qDebug() << "Copy of pointsArray1: ";
qDebug() << "Point_0 of array2: " << pointArray2.Element(0).GetX() << "," << pointArray2.Element(0).GetY();
qDebug() << "Point_1 of array2: " << pointArray2.Element(1).GetX() << "," << pointArray2.Element(1).GetY();
pointArray1.Element(0).Move(25,30); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
pointArray1.Element(1).Move(35,40); // 通過(guò)指針訪問(wèn)數(shù)組元素的成員
qDebug() << "After the moving of pointArray1: ";
qDebug() << "Point_0 of array2: " << pointArray2.Element(0).GetX() << "," << pointArray2.Element(0).GetY();
qDebug() << "Point_1 of array2: " << pointArray2.Element(1).GetX() << "," << pointArray2.Element(1).GetY();
return a.exec();
}
//運(yùn)行結(jié)果:
深拷貝運(yùn)行結(jié)果