八章節(jié)引用和拷貝構(gòu)造函數(shù)PPT學(xué)習(xí)教案_第1頁(yè)
八章節(jié)引用和拷貝構(gòu)造函數(shù)PPT學(xué)習(xí)教案_第2頁(yè)
八章節(jié)引用和拷貝構(gòu)造函數(shù)PPT學(xué)習(xí)教案_第3頁(yè)
八章節(jié)引用和拷貝構(gòu)造函數(shù)PPT學(xué)習(xí)教案_第4頁(yè)
八章節(jié)引用和拷貝構(gòu)造函數(shù)PPT學(xué)習(xí)教案_第5頁(yè)
已閱讀5頁(yè),還剩27頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、會(huì)計(jì)學(xué)1八章節(jié)引用和拷貝構(gòu)造函數(shù)八章節(jié)引用和拷貝構(gòu)造函數(shù)第1頁(yè)/共32頁(yè)第2頁(yè)/共32頁(yè)#include using namespace std;int y;int& r = y; / 引用被創(chuàng)建時(shí),必須聯(lián)系到某個(gè)存儲(chǔ)單元引用被創(chuàng)建時(shí),必須聯(lián)系到某個(gè)存儲(chǔ)單元 const int & q = 12; / (1) 編譯器分配一個(gè)存儲(chǔ)單元,編譯器分配一個(gè)存儲(chǔ)單元, / 然后然后q q跟該單元相聯(lián)系跟該單元相聯(lián)系int x = 0; / (2)int & a = x; / (3)int main() cout x = x , a = a endl; a+; cout x = x

2、 , a = a endl; /:第3頁(yè)/共32頁(yè)n引用:就像是能引用:就像是能自動(dòng)自動(dòng)被編譯器被編譯器間接引用間接引用的的constconst指針指針。第4頁(yè)/共32頁(yè)第5頁(yè)/共32頁(yè)第6頁(yè)/共32頁(yè)第7頁(yè)/共32頁(yè) void f(X &) void g(const X &) int main() X varxobj(1); const X cnstobj(10); /! f(cnstobj); / Error g(cnstobj); /:第8頁(yè)/共32頁(yè)第9頁(yè)/共32頁(yè)返回值返回值(寄存器寄存器/對(duì)象地址對(duì)象地址)實(shí)在參數(shù)實(shí)在參數(shù)返回地址返回地址保存的機(jī)器狀態(tài)保存的機(jī)器狀態(tài)

3、局部數(shù)據(jù)局部數(shù)據(jù)臨時(shí)數(shù)據(jù)臨時(shí)數(shù)據(jù)函數(shù)返回值,通常是對(duì)象的地址函數(shù)返回值,通常是對(duì)象的地址主調(diào)函數(shù)傳遞給被調(diào)函數(shù)的參數(shù)值主調(diào)函數(shù)傳遞給被調(diào)函數(shù)的參數(shù)值函數(shù)返回后的下一條執(zhí)行語(yǔ)句。也指示主調(diào)函數(shù)的活動(dòng)記錄函數(shù)返回后的下一條執(zhí)行語(yǔ)句。也指示主調(diào)函數(shù)的活動(dòng)記錄第10頁(yè)/共32頁(yè)把參數(shù)直把參數(shù)直接壓棧接壓棧從寄從寄存器存器中獲中獲得返得返回值回值第11頁(yè)/共32頁(yè)struct Big char buf50; int i; long d; B, B2;Big bigfun(Big b) b.i = 100; / Do something to the argument return b;int main(

4、) B2 = bigfun(B); /:第12頁(yè)/共32頁(yè)注注1注注2注:注:1. N_SPUSH1. N_SPUSH是一個(gè)把是一個(gè)把B B的的地址地址壓棧的輔助壓棧的輔助函數(shù);函數(shù);2. B22. B2的地址作為返回值也被壓棧的地址作為返回值也被壓棧第13頁(yè)/共32頁(yè)Obj AObj B第14頁(yè)/共32頁(yè)第15頁(yè)/共32頁(yè)第16頁(yè)/共32頁(yè)#include using namespace std;class HowMany static int objectCount;public: HowMany() objectCount+; static void print(const string

5、& msg = ) if(msg.size() != 0) cout msg : ; cout objectCount = objectCount endl; HowMany() objectCount-; print(HowMany(); 第17頁(yè)/共32頁(yè);int HowMany:objectCount = 0;/ 按值傳遞和返回按值傳遞和返回HowMany f(HowMany x) x.print(x argument inside f(); return x;int main() HowMany h; HowMany:print(after construction of h)

6、; HowMany h2 = h; HowMany:print(after h2=h); /:第18頁(yè)/共32頁(yè)第19頁(yè)/共32頁(yè)第20頁(yè)/共32頁(yè)第21頁(yè)/共32頁(yè)#include using namespace std;class HowMany2 string name; / Object identifier static int objectCount;public: HowMany2(const string& id = ) : name(id) +objectCount; print(HowMany2(); HowMany2() -objectCount; print(H

7、owMany2(); 第22頁(yè)/共32頁(yè)/ The copy-constructor: HowMany2(const HowMany2& h) : name() name += copy; +objectCount; print(HowMany2(const HowMany2&); void print(const string& msg = ) const if(msg.size() != 0) cout msg endl; cout t name : objectCount = objectCount endl; ;int HowMany2:object

8、Count = 0;第23頁(yè)/共32頁(yè)Howmany2() h:objectCount = 1Entering f()Howmany2(const Howmany2 &) h copy: objectCount = 2X argument inside f() h copy: objectCount = 2Returning from f()Howmany2(const Howmany2 &) h copy copy : objectCount = 3howmany2() h copy : objectCount = 2h2 after call to f() h copy c

9、opy : objectCount = 2Call f(), no return valueHowmany2(const Howmany2 &) h copy: objectCount = 3X argument inside f() h copy: objectCount = 3Returning from f()/ 臨時(shí)對(duì)象臨時(shí)對(duì)象Howmany2(const Howmany2 &) h copy copy: objectCount = 4 howmany2() h copy : objectCount = 3 howmany2() h copy copy : object

10、Count = 2After call to f()howmany2() / h2 h copy copy : objectCount = 1howmany2() / h h : objectCount =0第24頁(yè)/共32頁(yè)class Xprivate: X(const X &) ;X x1;X x2=x1; / error , 不能調(diào)用私有成員函數(shù)不能調(diào)用私有成員函數(shù)第25頁(yè)/共32頁(yè)class Cstring char * str; int sz;public: Cstring(int size) sz=size; str = new char sz; Cstring(const

11、 Cstring & obj) ;/ ;Cstring s1(10); ; Cstring s2(s1);第26頁(yè)/共32頁(yè)Cstring:Cstring(const Cstring & obj) sz= obj.sz; str = new charsz; memcpy(str, obj.str, sz) ; ;第27頁(yè)/共32頁(yè)第28頁(yè)/共32頁(yè)#include using namespace std;class Widget void f(int) const cout Widget:f()n; void g(int) const cout Widget:g()n; void h(int) const cout Widget:h()n; void i(int) const cout Widget:i()n; enum cnt = 4 ; void (Widget:*fptrcnt)(int) const; public: Widget() fptr0 = &Widget:f; / Full spec required fptr1 = &Widget:g; fptr2 = &Widget:h; fptr3 = &Widget:i;第29

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論