C++標準轉換運算符dynamic_第1頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

2、又和reinterpret_cast是全都的,但事實上,它們還是存在著很大的差別。 還是用代碼來說明,讓編譯器來解釋吧。 / / 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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論