ImageVerifierCode 换一换
格式:DOCX , 页数:23 ,大小:185.83KB ,
资源ID:18578971      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-18578971.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据类型转换.docx)为本站会员(b****0)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

数据类型转换.docx

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

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

3、1f9f01b866e (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 010D

4、1818 movsd 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 010D183

5、2 call 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 (010D

6、111Dh) 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.141

7、593注意:数据类型窄化转换时,注意数据溢出及丢失。(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:c

8、pp view 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)单参数构造函

9、数(2)赋值操作符(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 Con

10、version 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

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

12、t dval 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_cas

13、ta、 类层次结构中基类和派生类之间指针或者引用的转换。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

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

15、ndl; class 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

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

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

19、1:cpp 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) i

20、nt a20; 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,

21、配置: Debug 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

22、): note: 与指向的类型无关;转换要求 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

24、 char * 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 =

25、 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; int m_i

26、Num; ; int main(int argc, char* argv) A a0; const A &a1 = a0; A a2 = const_cast(a1); /常量引用转为非常量引用 a2.m_iNum = 200; cout a0.m_iNum endl; cout a1.m_iNum endl; cout a2.m_iNum endl; return 0; 其运行结果:11200c、常量对象(或基本类型)不可以被转换成非常量对象(或基本类型)eg:cpp view plain copy#include using std:cout; using std:endl; class A public: A() m_iNum = 1; int m_iNum; ; int main(int ar

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2