十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
有三種情況會(huì)產(chǎn)生復(fù)制構(gòu)造函數(shù)的調(diào)用!
在代碼中只要產(chǎn)生臨時(shí)對(duì)象都會(huì)調(diào)用復(fù)制構(gòu)造函數(shù)!
在代碼中顯示
#include
using namespace std;
class Location{
public:
Location(int a, int b){ x = a; y = b; }
Location( const Location& lc){
cout << "call copy_constructor" << endl;
x = lc.getx();//這里必須把getx()設(shè)為常量函數(shù)才可以被調(diào)用
y = lc.y;
}
~Location(){
cout << "object destroyed" << endl;
}
int getx()const{//只有定義為常函數(shù)才可以被常量對(duì)象使用
return x;
}
int gety()const{
return y;
}
void setx(int a){ x = a; }
void sety(int b){ y = b; }
private:
int x, y;
};
void f(Location p){//1.這里會(huì)產(chǎn)生調(diào)用復(fù)制構(gòu)造函數(shù)!因?yàn)樾枰a(chǎn)生臨時(shí)對(duì)象
p.setx(5);
p.sety(6);
cout << "x=" << p.getx() << " y=" << p.gety() << endl;
}
Location g(){//2.這里會(huì)產(chǎn)生調(diào)用復(fù)制構(gòu)造函數(shù)
Location p(4, 5);
return p;
}
Location& h(){//這里是引用所以不產(chǎn)生臨時(shí)對(duì)象,所以不調(diào)用復(fù)制構(gòu)造函數(shù)
cout << "h()" << endl;
Location p(4, 6);
return p;
}
void func(){
Location location1(2, 3);
Location location2 = location1;//3.這里也產(chǎn)生調(diào)用復(fù)制構(gòu)造函數(shù)
cout << "location.x=" << location2.getx() << endl;
cout << "location.y=" << location2.gety() << endl;
f(location2);
cout << "location.y=" << location2.gety() << endl;
location2 = g();
cout << "location.y=" << location2.gety() << endl;
location2 = h();
}
int main(){
func();//作用是使得所有的對(duì)象都可以析構(gòu)!
system("pause");
return 0;
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。