複製建構函式例項

2022-04-11 03:03:07 字數 1093 閱讀 6835

[cpp] #include  

#include  

#include  

#include  

#include  

#include  

#include  

#include  

using namespace std; 

class point 

;     point(point &p); 

private: 

double x, y; 

};  

point::point(point &p):x(p.x), y(p.y)   

class line 

; //point有形參,所以只能用初始化列表初始化  

//需要用初始化列表初始化:1.內嵌物件有參數列 2.常成員,需要繫結乙個物件  

line::line(point mp1, point mp2):p1(mp1), p2(mp2)   

line::line(line &l):p1(l.p1), p2(l.p2)   

int main() 

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

class point

; point(point &p);

private:

double x, y;

};point::point(point &p):x(p.x), y(p.y)

class line

;//point有形參,所以只能用初始化列表初始化

//需要用初始化列表初始化:1.內嵌物件有參數列 2.常成員,需要繫結乙個物件

line::line(point mp1, point mp2):p1(mp1), p2(mp2)

line::line(line &l):p1(l.p1), p2(l.p2)

int main()

執行結果如下:

摘自 飄過的小牛

c 複製建構函式例項

include using namespace std 複製建構函式只有乙個引數,即對同類物件的引用 形如 x x x 或x x const x 二者選一 如果沒有定義複製建構函式,編譯器會生成預設複製建構函式,預設是完成複製功能 複製建構函式不一定要定義成乙個完成複製功能的函式,完全由你自己決定 ...

複製建構函式

今天回看了前面的內容,發現這一章掌握的不夠好,就重看了一遍,順便總結一下 無規律總結 複製建構函式用於複製物件,即可以初始化物件,也可以將複製得到的物件作為實參傳遞給函式,多用於初始化。當我們這樣寫 string null bulk 9 9 9 9 在建立null bulk時編譯器先呼叫string...

複製建構函式

拷貝建構函式的標準寫法如下 class base base const base b 上述寫法見得最多,甚至你認為理所當然。那麼如果我們不寫成引用傳遞呢,而是值傳遞,那麼會怎樣?class base base const base b 編譯出錯 error c2652 base illegal co...