




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、數(shù)據(jù)類型轉換一、隱式類型轉換1)簡單數(shù)據(jù)類型(1)算術運算轉換為最寬的數(shù)據(jù)類型eg:cpp view plain copy#include <iostream> using std:cout; using std:endl; int main(int argc, char* argv) int ival = 3; double dval = 3.14159; cout << ival + dval << endl;/ival被提升為double類型 return 0; 其運行結果:6.14159int main(int argc, char* argv)01
2、0D17D0 push ebp 010D17D1 mov ebp,esp 010D17D3 sub esp,0DCh 010D17D9 push ebx 010D17DA push esi 010D17DB push edi 010D17DC lea edi,ebp-0DCh 010D17E2 mov ecx,37h 010D17E7 mov eax,0CCCCCCCCh 010D17EC rep stos dword ptr es:edi int ival = 3;010D17EE mov dword ptr ival,3 double dval = 3.14159;010D17F5 mov
3、sd xmm0,mmword ptr _real400921f9f01b866e (010D6B30h) 010D17FD movsd mmword ptr dval,xmm0 cout << ival + dval << endl;/ival被提升為double類型010D1802 mov esi,esp 010D1804 push offset std:endl<char,std:char_traits<char> > (010D1064h) 010D1809 cvtsi2sd xmm0,dword ptr ival 010D180E add
4、sd xmm0,mmword ptr dval 010D1813 mov edi,esp 010D1815 sub esp,8 010D1818 movsd mmword ptr esp,xmm0 010D181D mov ecx,dword ptr _imp_?coutstd3V?$basic_ostreamDU?$char_traitsDstd1A (010D90A8h) 010D1823 call dword ptr _imp_std:basic_ostream<char,std:char_traits<char> >:operator<< (010D
5、90A0h) 010D1829 cmp edi,esp 010D182B call _RTC_CheckEsp (010D111Dh) 010D1830 mov ecx,eax 010D1832 call dword ptr _imp_std:basic_ostream<char,std:char_traits<char> >:operator<< (010D90A4h) 010D1838 cmp esi,esp 010D183A call _RTC_CheckEsp (010D111Dh) return 0;010D183F xor eax,eax 010
6、D1841 pop edi 010D1842 pop esi 010D1843 pop ebx 010D1844 add esp,0DCh 010D184A cmp ebp,esp 010D184C call _RTC_CheckEsp (010D111Dh) 010D1851 mov esp,ebp 010D1853 pop ebp 010D1854 ret (2)賦值轉換為被賦值對象的類型,但不會改變賦值對象的數(shù)據(jù)類型。eg:cpp view plain copy#include <iostream> using std:cout; using std:endl; int ma
7、in(int argc, char* argv) int ival = 4; double dval = 3.14159; ival = dval; / double->int cout << dval << endl; cout << ival << endl; return 0; 其運行結果:3.141593注意:數(shù)據(jù)類型窄化轉換時,注意數(shù)據(jù)溢出及丟失。(3)函數(shù)傳參 當實參與形參數(shù)據(jù)類型不同時,轉換為形參數(shù)據(jù)類型。eg:cpp view plain copy#include <iostream> using std:cou
8、t; using std:endl; double square(double dval); int main(int argc, char* argv) cout << square(5) << endl; return 0; double square(double dval) return dval * dval; 其運行結果:25(4)函數(shù)返回當返回類型與表達式類型不同時,轉換為返回類型。eg:cpp view plain copy#include <iostream> using std:cout; using std:endl; double d
9、ifference(int ival1, int ival2); int main(int argc, char* argv) int ival1 = 2; int ival2 = 3; cout << difference(2, 3) << endl; return 0; double difference(int ival1, int ival2) return ival1 - ival2; /返回值被提升為double類型 其運行結果:-12)類類型(1)單參數(shù)構造函數(shù)(2)賦值操作符(3)類型轉換操作符eg:cpp view plain copy/ implic
10、it conversion of classes: #include <iostream> using std:cout; using std:endl; class A ; class B public: / conversion from A (constructor) B(const A& x) cout << "Conversion from A (constructor)" << endl; / conversion from A (assignment) B& operator= (const A& x
11、) cout << "Conversion from A (assignment)" << endl; return *this; / conversion to A (type-cast operator) operator A() cout << "Conversion to A (type-cast operator)" << endl; return A(); ; int main(int argc, char* argv) A foo; B bar = foo; / calls construct
12、or bar = foo; / calls assignment foo = bar; / calls type-cast operator return 0; 其運行結果:Conversion from A (constructor)Conversion from A (assignment)Conversion to A (type-cast operator)二、顯示類型轉換1)C風格dst = (T)srceg:cpp view plain copy#include <iostream> using std:cout; using std:endl; int main(in
13、t argc, char* argv) int ival; double dval = 3.14159; ival = (int)dval; / double->int cout << dval << endl; cout << ival << endl; return 0; 其運行結果:3.1415932)函數(shù)風格dst = T(src)eg:cpp view plain copy#include <iostream> using std:cout; using std:endl; int main(int argc, cha
14、r* argv) int ival; double dval = 3.14159; ival = int(dval); / double->int cout << dval << endl; cout << ival << endl; return 0; 其運行結果:3.1415933)(1)static_casta、 類層次結構中基類和派生類之間指針或者引用的轉換。up-casting (把派生類的指針或引用轉換成基類的指針或者引用表示)是安全的;down-casting(把基類指針或引用轉換成子類的指針或者引用)是不安全的。b、基本數(shù)據(jù)
15、類型之間的轉換c、把空指針轉換成目標類型的空指針(null pointer)d、 把任何類型的表達式轉換成void類型注意:不能轉換掉表達式的const、volitale或者_unaligned屬性。eg1:cpp view plain copy#include <iostream> using std:cout; using std:endl; class Dummy double i, j; ; class Addition int x, y; public: Addition(int a, int b) x = a; y = b; int result() return x
16、+ y; ; int main(int argc, char* argv) Dummy d; Addition * padd; padd = (Addition*)&d; cout << padd->result(); return 0; 其運行結果:-1717986920不做類型檢查,轉換沒有安全性eg2:cpp view plain copy#include <iostream> using std:cout; using std:endl; class Dummy double i, j; ; class Addition int x, y; pub
17、lic: Addition(int a, int b) x = a; y = b; int result() return x + y; ; int main(int argc, char* argv) Dummy d; Addition * padd; padd = static_cast<Addition*>(&d); cout << padd->result(); return 0; (2)dynamic_cast轉換類型與表達式類型相同,必須同時是類的指針、類的引用或void *.用于類的上行、下行及交叉轉換。一般情況下,dynamic_cast用
18、于具有多態(tài)性的類(即有虛函數(shù)的類)的類型轉換。a、上行轉換時,其與static_cast效果相同;b、下行轉換時,其具有類型轉換的功能,比static_cast更安全;c、交叉轉換時,其轉換成空指針,而static_cast則不允許轉換。注意:不能轉換掉表達式的const、volitale或者_unaligned屬性。eg:cpp view plain copy/ dynamic_cast #include <iostream> #include <exception> using std:cout; using std:endl; using std:exceptio
19、n; class Base virtual void dummy() ; class Derived : public Base int a; ; int main(int argc, char* argv) try Base * pba = new Derived; Base * pbb = new Base; Derived * pd; pd = dynamic_cast<Derived*>(pba); if (pd = 0) cout << "Null pointer on first type-cast.n" pd = dynamic_cas
20、t<Derived*>(pbb); if (pd = 0) cout << "Null pointer on second type-cast.n" catch (exception& e) cout << "Exception: " << e.what(); return 0; 其運行結果:Null pointer on second type-cast.(3)reinterpret_cast轉換一個指針為其他類型的指針,也允許將一個指針轉換為整數(shù)類型,反之亦然。這個操作符能夠在非相關的類型之間進
21、行轉換。操作結果只是簡單的從一個指針到別的指針的值的二進制拷貝,在類型之間指向的內(nèi)容不做任何類型的檢查和轉換。這是一個強制轉換。使用時有很大的風險,慎用之。注意:不能轉換掉表達式的const、volitale或者_unaligned屬性。eg1:cpp view plain copy/ expre_nterpret_cast_Operator.cpp / compile with: /EHsc #include <iostream> using std:cout; using std:endl; / Returns a hash code based on an address u
22、nsigned short Hash(void *p) unsigned int val = reinterpret_cast<unsigned int>(p); return (unsigned short)(val (val >> 16); int main(int argc, char* argv) int a20; for (int i = 0; i < 20; i+) cout << Hash(a + i) << endl; eg2:cpp view plain copy#include <iostream> stru
23、ct Foo ; struct Bar ; int main() Foo* f = new Foo; Bar* b1 = f; Bar* b2 = static_cast<Bar*>(f); Bar* b3 = dynamic_cast<Bar*>(f); Bar* b4 = const_cast<Bar*>(f); Bar* b5 = reinterpret_cast<Bar*>(f); return 0; 1>- 已啟動全部重新生成: 項目: test, 配置: Debug Win32 -1> main.cpp1>f:wor
24、kspacetesttestmain.cpp(10): error C2440: “初始化”: 無法從“Foo *”轉換為“Bar *”1> f:workspacetesttestmain.cpp(10): note: 與指向的類型無關;轉換要求 reinterpret_cast、C 樣式轉換或函數(shù)樣式轉換1>f:workspacetesttestmain.cpp(11): error C2440: “static_cast”: 無法從“Foo *”轉換為“Bar *”1> f:workspacetesttestmain.cpp(11): note: 與指向的類型無關;轉換要
25、求 reinterpret_cast、C 樣式轉換或函數(shù)樣式轉換1>f:workspacetesttestmain.cpp(12): error C2683: “dynamic_cast”:“Foo”不是多態(tài)類型1> f:workspacetesttestmain.cpp(3): note: 參見“Foo”的聲明1>f:workspacetesttestmain.cpp(13): error C2440: “const_cast”: 無法從“Foo *”轉換為“Bar *”1> f:workspacetesttestmain.cpp(13): note: 與指向的類型無
26、關;轉換要求 reinterpret_cast、C 樣式轉換或函數(shù)樣式轉換= 全部重新生成: 成功 0 個,失敗 1 個,跳過 0 個 =(4)const_cast修改類型的const或volatile屬性a、常量指針被轉化成非常量指針,轉換后指針指向原來的變量(即轉換后的指針地址不變)eg1:cpp view plain copy/ const_cast #include <iostream> using std:cout; using std:endl; void print(char * str) cout << str << endl; int ma
27、in(int argc, char* argv) const char * c = "sample text" print(const_cast<char *> (c); return 0; 其運行結果:sample texteg2:cpp view plain copy#include <iostream> using std:cout; using std:endl; class A public: A() m_iNum = 0; int m_iNum; ; int main(int argc, char* argv) /1、指針指向類 cons
28、t A *pca1 = new A; A *pa2 = const_cast<A*>(pca1); /常量對象轉換為非常量對象 pa2->m_iNum = 200; /轉換后指針指向原來的對象 cout << pca1->m_iNum << " " << pa2->m_iNum << endl; /200 200 /2、指針指向基本類型 const int ica = 100; int * ia = const_cast<int *>(&ica); *ia = 200; cou
29、t << *ia << " " << ica << endl; /200 100 return 0; 其運行結果:200 200200 100b、常量引用轉為非常量引用eg:cpp view plain copy#include <iostream> using std:cout; using std:endl; class A public: A() m_iNum = 1; int m_iNum; ; int main(int argc, char* argv) A a0; const A &a1 = a0; A a2 = const_cast<A&>(a1); /常量引用轉為非常量引用 a2.m_iNum = 200; cout << a0.m_iNum << endl; cout << a1.m_iNum << endl; cout << a2.m_iNum << endl; return 0; 其運行結果:11200c、
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 合股賣水泥合同范本
- 勞務分包單位合同范本
- 勞務合同范本車輛
- 微信租房合同范本
- 與單位簽正式合同范本
- 廠內(nèi)車間出租合同范本
- 化肥生產(chǎn)合同范本
- 做建設合同范本
- 合同范本嬰兒車
- 分期付款機器買賣合同范本
- 保密風險評估報告
- 道路建筑材料電子教案(全)
- 《尹定邦設計學概論》試題及答案
- 黃河流域生態(tài)保護和高質(zhì)量發(fā)展知識競賽試題及答案(共52題)
- 1、1~36號元素電子排布式、排布圖
- 國網(wǎng)新聞宣傳與企業(yè)文化管理專責題庫匯總-下(判斷、簡答題)
- 職業(yè)衛(wèi)生調(diào)查表
- 某安裝公司績效考核標準表
- 免疫學檢測技術的基本原理優(yōu)秀課件
- 2023年北京市實驗動物上崗證培訓考試題庫完美精編版
- 第一講 文化概論
評論
0/150
提交評論