先看一段代碼,摘自知乎:
#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;
}
輸出結(jié)果為:
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);
}
調(diào)用構(gòu)造函數(shù)Line::Line
時(shí)昼伴,由于采用傳值的方式准颓,需要生成實(shí)參myp1
,myp2
的副本凉馆,可以理解為初始化Point xp1(myp1)
, Point xp2(myp2)
薪寓;于是調(diào)用了兩次拷貝構(gòu)造函數(shù),同理澜共,初始化列表中p1(xp1), p2(xp2)也調(diào)用了兩次拷貝構(gòu)造函數(shù)向叉。綜上,調(diào)用4次拷貝構(gòu)造函數(shù)(都在進(jìn)入函數(shù)體之前完成)嗦董。
如果將構(gòu)造函數(shù)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);
}
輸出結(jié)果為:
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
少調(diào)用了兩次拷貝構(gòu)造函數(shù)母谎,轉(zhuǎn)而執(zhí)行Point 的構(gòu)造函數(shù),同樣也是在進(jìn)入函數(shù)體之前完成京革。這里并不是p1 = xp1; p2 = xp2
執(zhí)行的時(shí)候再執(zhí)行的Point
的構(gòu)造函數(shù)奇唤,這一行只完成賦值工作;調(diào)用Line
的構(gòu)造函數(shù)時(shí)匹摇,由于沒有初始化列表咬扇,將通過默認(rèn)參數(shù)的構(gòu)造函數(shù)首先初始化Line
的成員Point p1, p2
, 產(chǎn)生兩條輸出,與Line::Line
的函數(shù)體內(nèi)p1,p2
被賦值無關(guān)廊勃!可以檢測懈贺,刪掉賦值的語句,仍會(huì)調(diào)用兩次Point
的構(gòu)造函數(shù)供搀!
補(bǔ)充:
Point p1 = p2;
與
Point p1;
p1 = p2;
是不同的隅居!
第一種寫法調(diào)用的時(shí)拷貝構(gòu)造函數(shù),相當(dāng)于Point p1(p2)
葛虐,這里的等號并不是賦值操作!;而第二種寫法首先用默認(rèn)的構(gòu)造函數(shù)初始化p1
(需要定義默認(rèn)的構(gòu)造函數(shù))胎源,再將p2
的值賦值給p1
,所以也不會(huì)調(diào)用拷貝構(gòu)造函數(shù)屿脐。
一般一個(gè)類定義了拷貝構(gòu)造函數(shù)涕蚤,就應(yīng)該相應(yīng)的重載賦值運(yùn)算符,以確保兩者含義一致的诵。