C++成員函數(shù)有類對(duì)象時(shí)構(gòu)造的順序

先看一段代碼,摘自知乎

#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í)參myp1myp2的副本,可以理解為初始化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)!可以檢測(cè),刪掉賦值的語句,仍會(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),這里的等號(hào)并不是賦值操作!;而第二種寫法首先用默認(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)算符,以確保兩者含義一致。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容