随笔分类 -  类型转换

摘要:#include <iostream>using namespace std;int main(){ int a=1; char b = (char)(a+48); int c = (int)(b-48); if(b == '1') cout<<b<<endl; if(c == 1) cout<<c<<endl; getchar();} 阅读全文
posted @ 2012-11-02 19:46 byfei 阅读(161) 评论(0) 推荐(0) 编辑
摘要:关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++ 之父的《C++ 的设计和演化》。最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_cast, dynamic_cast。标准C++中有四个类型转换符:static_cast、dynamic_cast、reinterpret_cast、和const_cast。下面对它们一一进行介绍。static_cast用法:static_cast < type-id > ( expression )该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。 阅读全文
posted @ 2012-06-28 11:32 byfei 阅读(394) 评论(0) 推荐(0) 编辑