先看一段代碼,摘自知乎:
#include <iostream>
#include <cmath>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
x=xx;
y=yy;
cout << "Calling constructor of Point" << endl;
}
Point(Point & p);
int getx() {return x;}
int gety() {return y;}
private:
int x,y;
};
Point::Point(Point & p){
x=p.x;
y=p.y;
cout<<"Calling the copy constructor of Point"<<endl;
}
class Line{
public:
Line(Point xp1,Point xp2);
Line(Line & l);
double getLen() {return len;}
private:
Point p1,p2;
double len;
};
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){
cout<<"Calling constructor of Line"<<endl;
double x=static_cast<double>(p1.getx()-p2.getx());
double y=static_cast<double>(p1.gety()-p2.gety());
len=sqrt(x*x+y*y);
}
Line::Line(Line & l):p1(l.p1),p2(l.p2){
cout<<"Calling the copy constructor of Line"<<endl;
len=l.len;
}
int main(){
Point myp1(1,1),myp2(4,5);
cout << endl;
Line line(myp1,myp2);
return 0;
}
輸出結果為:
Calling constructor of Point
Calling constructor of Point
Calling the copy constructor of Point
Calling the copy constructor of Point
Calling the copy constructor of Point
Calling the copy constructor of Point
Calling constructor of Line
解釋Line line(myp1 myp2)
一句:
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){
cout<<"Calling constructor of Line"<<endl;
double x=static_cast<double>(p1.getx()-p2.getx());
double y=static_cast<double>(p1.gety()-p2.gety());
len=sqrt(x*x+y*y);
}
調用構造函數Line::Line
時,由于采用傳值的方式,需要生成實參myp1
,myp2
的副本,可以理解為初始化Point xp1(myp1)
, Point xp2(myp2)
;于是調用了兩次拷貝構造函數,同理,初始化列表中p1(xp1), p2(xp2)也調用了兩次拷貝構造函數。綜上,調用4次拷貝構造函數(都在進入函數體之前完成)。
如果將構造函數Line:Line
改為:
Line::Line(Point xp1,Point xp2){
p1 = xp1; p2 = xp2; // 不用初始化列表的方式
cout<<"Calling constructor of Line"<<endl;
double x=static_cast<double>(p1.getx()-p2.getx());
double y=static_cast<double>(p1.gety()-p2.gety());
len=sqrt(x*x+y*y);
}
輸出結果為:
Calling constructor of Point
Calling constructor of Point
Calling the copy constructor of Point
Calling constructor of Point
Calling constructor of Point
Calling constructor of Line
少調用了兩次拷貝構造函數,轉而執行Point 的構造函數,同樣也是在進入函數體之前完成。這里并不是p1 = xp1; p2 = xp2
執行的時候再執行的Point
的構造函數,這一行只完成賦值工作;調用Line
的構造函數時,由于沒有初始化列表,將通過默認參數的構造函數首先初始化Line
的成員Point p1, p2
, 產生兩條輸出,與Line::Line
的函數體內p1,p2
被賦值無關!可以檢測,刪掉賦值的語句,仍會調用兩次Point
的構造函數!
補充:
Point p1 = p2;
與
Point p1;
p1 = p2;
是不同的!
第一種寫法調用的時拷貝構造函數,相當于Point p1(p2)
,這里的等號并不是賦值操作!;而第二種寫法首先用默認的構造函數初始化p1
(需要定義默認的構造函數),再將p2
的值賦值給p1
,所以也不會調用拷貝構造函數。
一般一個類定義了拷貝構造函數,就應該相應的重載賦值運算符,以確保兩者含義一致。