C++標(biāo)準(zhǔn)轉(zhuǎn)換運(yùn)算符dynamic_第1頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡(jiǎn)介

1、c+標(biāo)準(zhǔn)轉(zhuǎn)換運(yùn)算符dynamicdynamic_cast new_type (pression) dynamic_cast運(yùn)算符,應(yīng)當(dāng)算是四個(gè)里面最特別的一個(gè),由于它涉及到編譯器的屬性設(shè)置,而且牽扯到的面對(duì)對(duì)象的多態(tài)性跟程序運(yùn)行時(shí)的狀態(tài)也有關(guān)系,所以不能徹低的用法傳統(tǒng)的轉(zhuǎn)換方式來替代。但是也因此它是最常用,最不行缺少的一個(gè)運(yùn)算符。 與ic_cast一樣,dynamic_cast的轉(zhuǎn)換也需要目標(biāo)類型和源對(duì)象有一定的關(guān)系:繼承關(guān)系。 更精確的說,dynamic_cast是用來檢查兩者是否有繼承關(guān)系。因此該運(yùn)算符事實(shí)上只接受基于類對(duì)象的指針和引用的類轉(zhuǎn)換。從這個(gè)方面來看,似乎dynamic_cast

2、又和reinterpret_cast是全都的,但事實(shí)上,它們還是存在著很大的差別。 還是用代碼來說明,讓編譯器來解釋吧。 / / cast_operator_comparison.p / language: c+ / complier: vial studio 2010, xcode3.2.6 / platfo: macbook pro 2010 / appliion: none / author: ider, syracuse university er.cs / ilude sing include iostream using namespace std; class parents p

3、ublic: parents(string n=parent) name = n; virtual parents() virtual void speak() cout ti am name , i love my children. endl; void work() cout ti am name , i ne to work for my family. endl; protected: string name; class children : public parents public: children(string n=child):parents(n) virtual chi

4、ldren() virtual void speak() cout ti am name , i love my parents. endl; *children inherit work() method from parents, *it could be treated like part- job. void study() cout ti am name , i need to study for future. endl; private: /string name; /inherit name member from parents class stranger public:

5、stranger(string n=stranger)name = n; virtual stranger() void self_introce() cout ti am a stranger endl; void speak() /cout i am a stranger endl; cout tdo not talk to name , is a stranger. endl; private: string name; int main() /* cast from child class to base class */ cout dynamic_cast from child cl

6、ass to base class: endl; children * daughter_d = new children(daughter who pretend to be my mother); parents * mother_d = dynamic_cast parents* (daughter_d); /right, cast with polymorphism mother_d- speak(); mother_d- work(); /mother_d- study(); /error, no such method cout static_cast from child cla

7、ss to base class: endl; children * son_s = new children(son who pretend to be my father); parents * father_s = static_cast parents* (son_s); /right, cast with polymorphism father_s- speak(); father_s- work(); /father_s- study(); /error, no such method cout endl; /* cast from base class to child clas

8、s */ cout dynamic_cast from base class to child class: endl; parents * father_d = new parents(father who pretend to be a my son); children * son_d = dynamic_cast children* (father_d); /no error, but not safe if (son_d) son_d- speak(); son_d- study(); ee cout tnull endl; cout static_cast from base cl

9、ass to child class: endl; parents * mother_s = new parents(mother who pretend to be a my daugher); children * daughter_s = static_cast children* (mother_s); /no error, but not safe if (daughter_s) daughter_s- speak(); daughter_s- study(); else cout tnull endl; cout endl; /* cast between non-related

10、class */ cout dynamic_cast to non-related class: endl; stranger* stranger_d = dynamic_cast stranger* (daughter_d); if (stranger_d) stranger_d- self_introduce(); stranger_d- speak(); else cout tnull endl; /stranger* stranger_s = static_cast stranger* (son_s); /error, invalid cast cout reinterpret_cast to non-related class: endl; stranger* stranger_r = reinterpret_cast stranger* (son_s); if (stranger_r) stranger_d- self_introduce();

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論