為什么在Line.h中實(shí)例化不需要傳入?yún)?shù)
想問下,在Line.h中,
Coordinate m_coorA;
Coordinate m_coorB;
這兩行代碼是類的成員變量,同時(shí)也是將Coordinate類中的兩個(gè)對(duì)象實(shí)例化了對(duì)吧。那么Coordinate的構(gòu)造函數(shù)如圖一寫成了
Coordinate::Coordinate(int x,int y)
{
m_iX=x;
m_iY=y;
cout<<"did it"<<endl;
}
這是有參數(shù)的構(gòu)造函數(shù),那不是矛盾了嗎?構(gòu)造函數(shù)有參數(shù),對(duì)應(yīng)的對(duì)象實(shí)例化的時(shí)候卻是沒有參數(shù)的
2019-08-31
在Line.h中,Coordinate m_coorA;Coordinate m_coorB; 僅僅是申明這個(gè)line類有這個(gè)成員,并沒有實(shí)例化,所以并沒執(zhí)行構(gòu)造函數(shù),只有實(shí)例化時(shí)才執(zhí)行構(gòu)造