下載本文檔
版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度鋁合金門窗工程勞務(wù)分包合同示范文本4篇
- 2025年環(huán)保型環(huán)衛(wèi)車輛及設(shè)備采購(gòu)合同3篇
- 2025年度苗圃技術(shù)員苗木市場(chǎng)分析與營(yíng)銷服務(wù)合同3篇
- 2025年度數(shù)字經(jīng)濟(jì)園區(qū)承包經(jīng)營(yíng)合同示范文本4篇
- 商業(yè)安全生產(chǎn)的文化塑造與實(shí)踐
- 2025版新能源發(fā)電項(xiàng)目工程監(jiān)理合同4篇
- 2025版五金配件進(jìn)出口貿(mào)易合同樣本3篇
- 二零二五年度酒店餐飲服務(wù)合同范本
- 2025年智能櫥柜研發(fā)與制造合作協(xié)議4篇
- 2025年度酒吧吧臺(tái)承包與酒吧市場(chǎng)調(diào)研與分析合同4篇
- 2024年萍鄉(xiāng)衛(wèi)生職業(yè)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫(kù)標(biāo)準(zhǔn)卷
- DB32-T 4444-2023 單位消防安全管理規(guī)范
- 臨床三基考試題庫(kù)(附答案)
- 人員密集場(chǎng)所消防安全管理培訓(xùn)
- JCT587-2012 玻璃纖維纏繞增強(qiáng)熱固性樹脂耐腐蝕立式貯罐
- 典范英語(yǔ)2b課文電子書
- 員工信息登記表(標(biāo)準(zhǔn)版)
- 春節(jié)工地停工復(fù)工計(jì)劃安排( 共10篇)
- 新教材人教版高中物理選擇性必修第二冊(cè)全冊(cè)各章節(jié)課時(shí)練習(xí)題及章末測(cè)驗(yàn)含答案解析(安培力洛倫茲力電磁感應(yīng)交變電流等)
- 中考數(shù)學(xué)試題(含答案)共12套
- 初級(jí)養(yǎng)老護(hù)理員培訓(xùn)全套
評(píng)論
0/150
提交評(píng)論