freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

堆與復(fù)制構(gòu)造函數(shù)ppt課件-資料下載頁

2025-04-28 23:04本頁面
  

【正文】 不可預(yù)料的錯(cuò)誤。 【 例 】 函數(shù)返回對(duì)象 include iostream include cstring using namespace std。 class my_string { char *s。 public: my_string(char *str)。 //普通構(gòu)造函數(shù) ~my_string() { if(s) delete [] s。 cout Freeing s\n。 } void show() { cout s \n。 } }。 【 例 】 (續(xù) 1) my_string::my_string(char *str) //普通構(gòu)造函數(shù) { s = new char[strlen(str)+1]。 coutNormal constructor called.\n。 strcpy(s, str)。 } my_string input( ) // 返回一個(gè) my_string類型的對(duì)象 { char instr[80]。 coutEnter a string: 。 cininstr。 my_string ob(instr)。 //調(diào)用普通構(gòu)造函數(shù) return ob。 } 【 例 】 (續(xù) 2) int main() { my_string obj=input()。 //調(diào)用默認(rèn)的復(fù)制構(gòu)造函數(shù) //在函數(shù) input()中將調(diào)用普通構(gòu)造函數(shù) ()。 //輸出的是垃圾數(shù)據(jù) return 0。 } 該程序運(yùn)行后的輸出結(jié)果如下: Enter a string: Hello Normal constructor called. Freeing s 葺葺葺葺葺葺葺葺葺葺輛 @(這是輸出的垃圾數(shù)據(jù),不同的系統(tǒng)中輸出的內(nèi)容會(huì)不相同) 【 例 】 使用自定義的復(fù)制構(gòu)造函數(shù)避免錯(cuò)誤 include iostream include cstring using namespace std。 class my_string { char *s。 public: my_string(char *str)。 //普通構(gòu)造函數(shù) my_string(const my_string amp。obj)。 //自定義的復(fù)制構(gòu)造函數(shù) ~my_string() { if(s) delete [] s。 cout Freeing s\n。 } void show() { cout s \n。 } }。 【 例 】 (續(xù) 1) my_string::my_string(char *str) //普通構(gòu)造函數(shù) { s = new char[strlen(str)+1]。 coutNormal constructor called.\n。 strcpy(s, str)。 } //自定義的復(fù)制構(gòu)造函數(shù) my_string::my_string(const my_string amp。obj) { s = new char[strlen()+1]。 strcpy(s, )。 coutCopy constructor called.\n。 } 【 例 】 (續(xù) 2) my_string input( ) // 返回一個(gè) my_string類型的對(duì)象 { char instr[80]。 coutEnter a string: 。 cininstr。 my_string ob(instr)。 //調(diào)用普通構(gòu)造函數(shù) return ob。 //調(diào)用復(fù)制構(gòu)造函數(shù) } int main() { my_string obj=input( )。 //調(diào)用復(fù)制構(gòu)造函數(shù) //在函數(shù) input()中將調(diào)用普通構(gòu)造函數(shù) ()。 return 0。 } 【 例 】 (續(xù) 3) 該程序運(yùn)行后的輸出結(jié)果如下: Enter a string: Hello Normal constructor called. Copy constructor called. Freeing s Hello Freeing s 一個(gè)經(jīng)驗(yàn) ? 如果你的類需要析構(gòu)函數(shù)來 釋放資源 ,則它也需要一個(gè)自定義的復(fù)制構(gòu)造函數(shù)。
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1