
下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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)換方式來(lái)替代。但是也因此它是最常用,最不行缺少的一個(gè)運(yùn)算符。 與ic_cast一樣,dynamic_cast的轉(zhuǎn)換也需要目標(biāo)類型和源對(duì)象有一定的關(guān)系:繼承關(guān)系。 更精確的說(shuō),dynamic_cast是用來(lái)檢查兩者是否有繼承關(guān)系。因此該運(yùn)算符事實(shí)上只接受基于類對(duì)象的指針和引用的類轉(zhuǎn)換。從這個(gè)方面來(lái)看,似乎dynamic_cast
2、又和reinterpret_cast是全都的,但事實(shí)上,它們還是存在著很大的差別。 還是用代碼來(lái)說(shuō)明,讓編譯器來(lái)解釋吧。 / / 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ú)特殊說(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 鄉(xiāng)村合作社與農(nóng)戶聯(lián)合開(kāi)發(fā)農(nóng)業(yè)技術(shù)項(xiàng)目協(xié)議
- 通信技術(shù)與信號(hào)處理練習(xí)題
- 技術(shù)標(biāo)準(zhǔn)制定合作協(xié)議
- 數(shù)學(xué)課本九章算術(shù)教案
- 教育資源分布報(bào)告表
- 西廂記的愛(ài)情悲劇征文
- 中學(xué)生國(guó)學(xué)經(jīng)典故事解讀
- 農(nóng)業(yè)旅游開(kāi)發(fā)實(shí)施方案
- 數(shù)據(jù)安全與隱私保護(hù)服務(wù)協(xié)議約定事項(xiàng)
- 業(yè)務(wù)往來(lái)預(yù)付款協(xié)議書(shū)
- 體育測(cè)量與評(píng)價(jià)-第二章-體育測(cè)量與評(píng)價(jià)的基礎(chǔ)理論課件
- 法律服務(wù)方案(投標(biāo))
- 轉(zhuǎn)移的危險(xiǎn)廢物性狀清單
- 高中英語(yǔ)-新外研版必修一unit5-The-Monarchs-Journey-公開(kāi)課reading課件
- 建設(shè)項(xiàng)目用地預(yù)審與選址意見(jiàn)課件講解
- 四年級(jí)公共安全教育全冊(cè)教案(海峽教育出版社)
- 工程結(jié)構(gòu)通用規(guī)范
- 《構(gòu)成基礎(chǔ)》PPT課件(190頁(yè)P(yáng)PT)
- 四年級(jí)道德與法治從中國(guó)制造到中國(guó)創(chuàng)造
- 2021-2022新教科版四年級(jí)科學(xué)下冊(cè)全一冊(cè)全部課件(共24課)
- 3 棄渣場(chǎng)施工方案
評(píng)論
0/150
提交評(píng)論