数据类型转换_第1页
数据类型转换_第2页
数据类型转换_第3页
数据类型转换_第4页
数据类型转换_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、数据类型转换一、隐式类型转换1)简单数据类型(1)算术运算转换为最宽的数据类型eg:cpp view plain copy#include 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)010D17D0 push ebp 010D17D1 mov ebp,es

2、p 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 movsd xmm0,mmword ptr _real400921f9f01

3、b866e (010D6B30h) 010D17FD movsd mmword ptr dval,xmm0 cout ival + dval endl;/ival被提升为double类型010D1802 mov esi,esp 010D1804 push offset std:endlchar,std:char_traits (010D1064h) 010D1809 cvtsi2sd xmm0,dword ptr ival 010D180E addsd xmm0,mmword ptr dval 010D1813 mov edi,esp 010D1815 sub esp,8 010D1818 m

4、ovsd mmword ptr esp,xmm0 010D181D mov ecx,dword ptr _imp_?coutstd3V?$basic_ostreamDU?$char_traitsDstd1A (010D90A8h) 010D1823 call dword ptr _imp_std:basic_ostreamchar,std:char_traits :operator (010D90A0h) 010D1829 cmp edi,esp 010D182B call _RTC_CheckEsp (010D111Dh) 010D1830 mov ecx,eax 010D1832 call

5、 dword ptr _imp_std:basic_ostreamchar,std:char_traits :operator (010D90A4h) 010D1838 cmp esi,esp 010D183A call _RTC_CheckEsp (010D111Dh) return 0;010D183F xor eax,eax 010D1841 pop edi 010D1842 pop esi 010D1843 pop ebx 010D1844 add esp,0DCh 010D184A cmp ebp,esp 010D184C call _RTC_CheckEsp (010D111Dh)

6、 010D1851 mov esp,ebp 010D1853 pop ebp 010D1854 ret (2)赋值转换为被赋值对象的类型,但不会改变赋值对象的数据类型。eg:cpp view plain copy#include using std:cout; using std:endl; int main(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注意:

7、数据类型窄化转换时,注意数据溢出及丢失。(3)函数传参 当实参与形参数据类型不同时,转换为形参数据类型。eg:cpp view plain copy#include using std:cout; 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)函数返回当返回类型与表达式类型不同时,转换为返回类型。eg:cpp vie

8、w plain copy#include using std:cout; using std:endl; double difference(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)单参数构造函数(2)赋值

9、操作符(3)类型转换操作符eg:cpp view plain copy/ implicit conversion of classes: #include 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) cout Conversio

10、n 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 constructor bar = foo; / calls assignment foo = bar; / calls type-cast operator retur

11、n 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 using std:cout; using std:endl; int main(int argc, char* argv) int ival; double dval = 3.14159; ival = (int)dval; / double-int cout dval

12、 endl; cout ival endl; return 0; 其运行结果:3.1415932)函数风格dst = T(src)eg:cpp view plain copy#include using std:cout; using std:endl; int main(int argc, char* 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、 类层

13、次结构中基类和派生类之间指针或者引用的转换。up-casting (把派生类的指针或引用转换成基类的指针或者引用表示)是安全的;down-casting(把基类指针或引用转换成子类的指针或者引用)是不安全的。b、基本数据类型之间的转换c、把空指针转换成目标类型的空指针(null pointer)d、 把任何类型的表达式转换成void类型注意:不能转换掉表达式的const、volitale或者_unaligned属性。eg1:cpp view plain copy#include using std:cout; using std:endl; class Dummy double i, j; ;

14、 class Addition int x, y; public: 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 = (Addition*)&d; cout result(); return 0; 其运行结果:-1717986920不做类型检查,转换没有安全性eg2:cpp view plain copy#include using std:cout; using std:endl; c

15、lass Dummy double i, j; ; class Addition int x, y; public: 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(&d); cout result(); return 0; (2)dynamic_cast转换类型与表达式类型相同,必须同时是类的指针、类的引用或void *.用于类的上行、下行及交叉转换。一般情况

16、下,dynamic_cast用于具有多态性的类(即有虚函数的类)的类型转换。a、上行转换时,其与static_cast效果相同;b、下行转换时,其具有类型转换的功能,比static_cast更安全;c、交叉转换时,其转换成空指针,而static_cast则不允许转换。注意:不能转换掉表达式的const、volitale或者_unaligned属性。eg:cpp view plain copy/ dynamic_cast #include #include using std:cout; using std:endl; using std:exception; class Base virtua

17、l 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(pba); if (pd = 0) cout Null pointer on first type-cast.n; pd = dynamic_cast(pbb); if (pd = 0) cout Null pointer on second type-cas

18、t.n; catch (exception& e) cout Exception: e.what(); return 0; 其运行结果:Null pointer on second type-cast.(3)reinterpret_cast转换一个指针为其他类型的指针,也允许将一个指针转换为整数类型,反之亦然。这个操作符能够在非相关的类型之间进行转换。操作结果只是简单的从一个指针到别的指针的值的二进制拷贝,在类型之间指向的内容不做任何类型的检查和转换。这是一个强制转换。使用时有很大的风险,慎用之。注意:不能转换掉表达式的const、volitale或者_unaligned属性。eg1:cpp

19、view plain copy/ expre_nterpret_cast_Operator.cpp / compile with: /EHsc #include using std:cout; using std:endl; / Returns a hash code based on an address unsigned short Hash(void *p) unsigned int val = reinterpret_cast(p); return (unsigned short)(val (val 16); int main(int argc, char* argv) int a20

20、; for (int i = 0; i 20; i+) cout Hash(a + i) endl; eg2:cpp view plain copy#include struct Foo ; struct Bar ; int main() Foo* f = new Foo; Bar* b1 = f; Bar* b2 = static_cast(f); Bar* b3 = dynamic_cast(f); Bar* b4 = const_cast(f); Bar* b5 = reinterpret_cast(f); return 0; 1- 已启动全部重新生成: 项目: test, 配置: De

21、bug Win32 -1 main.cpp1f:workspacetesttestmain.cpp(10): error C2440: “初始化”: 无法从“Foo *”转换为“Bar *”1 f:workspacetesttestmain.cpp(10): note: 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换1f:workspacetesttestmain.cpp(11): error C2440: “static_cast”: 无法从“Foo *”转换为“Bar *”1 f:workspacetesttestmain.cpp(11): not

22、e: 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换1f:workspacetesttestmain.cpp(12): error C2683: “dynamic_cast”:“Foo”不是多态类型1 f:workspacetesttestmain.cpp(3): note: 参见“Foo”的声明1f:workspacetesttestmain.cpp(13): error C2440: “const_cast”: 无法从“Foo *”转换为“Bar *”1 f:workspacetesttestmain.cpp(13): note: 与指向的类型无关

23、;转换要求 reinterpret_cast、C 样式转换或函数样式转换= 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 =(4)const_cast修改类型的const或volatile属性a、常量指针被转化成非常量指针,转换后指针指向原来的变量(即转换后的指针地址不变)eg1:cpp view plain copy/ const_cast #include using std:cout; using std:endl; void print(char * str) cout str endl; int main(int argc, char* argv) const char

24、* c = sample text; print(const_cast (c); return 0; 其运行结果:sample texteg2:cpp view plain copy#include using std:cout; using std:endl; class A public: A() m_iNum = 0; int m_iNum; ; int main(int argc, char* argv) /1、指针指向类 const A *pca1 = new A; A *pa2 = const_cast(pca1); /常量对象转换为非常量对象 pa2-m_iNum = 200; /转换后指针指向原来的对象 cout m_iNum m_iNum endl; /200 200 /2、指针指向基本类型 const int ica = 100; int * ia = const_cast(&ica); *ia = 200; cout *ia ica endl; /200 100 return 0; 其运行结果:200 200200 100b、常量引用转为非常量引用eg:cpp view plain copy#include using std:cout; using std:endl; class A public: A() m_iNum

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论