C/C++类型转换
1.隐式类型转换
1.1 隐式类型转换的规则
K & R A.6.5
Arithmetic Conversions(数值型间的转换)
First, if either operand is long double, the other is converted to long double.
Otherwise, if either operand is double, the other is converted to double.
Otherwise, if either operand is float, the other is converted to float.
Otherwise, the integral promotions are performed on both operands; then, if either operand is unsigned long int, the other is converted to unsigned long int.
Otherwise, if one operand is long int and the other is unsigned int, the effect depends on whether a long int can represent all values of an unsigned int; if so, the unsigned int operand is converted to long int; if not, both are converted to unsigned long int.
Otherwise, if one operand is long int, the other is converted to long int.
Otherwise, if either operand is unsigned int, the other is converted to unsigned int.
Otherwise, both operands have type int.
[包含浮点型的运算]
如果任意一个操作数是long double, 那么另一个要转换为 long double
如果任意一个操作数是double, 那么另一个要转换为 double
如果任意一个操作数是float, 那么另一个要转换为 float
[2个非浮点型的运算]
如果任意一个操作数是unsigned long int, 那么另一个要转换为unsigned long int
如果一个操作数是long int, 另一个是unsigned int, 如果long int可以表示结果,那么unsigned int要转换为long int,否则两个都转换为unsigned long int
如果任意一个操作数是long int, 那么另一个要转换为long int
如果任意一个操作数是unsigned int, 那么另一个要转换为unsigned int
除此之外,两个操作数都应是int
1.2 发生隐式类型转换的条件
① 有多种数据类型的算术表达式
② 将一种类型的数据赋值给另一种类型的变量.(将等号右边的结果 转换 成 左边变量的类型)
③ 函数调用中, 实参与形参类型不同,返回值类型 与 函数声明不同.(实参转成形参类型, 返回值转成 函数声明类型)
2.显示类型转换(强制类型转换)
2.1 显示转换的写法
写法主要有 2 中, 其中一种是C++独有
int a = ; //1. (type) exp ;C and C++
float b = (float)a; //2 type (exp) ;C++ only
float c = float (a);
2.2 C++强制类型转换运算符
在C++中,有 4 个强制类型转换运算符,分别是:static_cast、dynamic_cast、const_cast 和 reinterpret_cast。
2.2.1、static_cast
静态强制转换,用于实现任何标准类型间的转换。
2.2.2、dynamic_cast
动态强制转换,用于基类 和 派生类 对象之间的指针转换,以实现多态。
dynamic_cast是在程序运行的时刻实现的,其他强制转换实在编译时就完成了.
#include <iostream> class Base
{
public:
int varB;
virtual void func(void){std::cout<<"Base::func"<<std::endl;}
};
class Derived:public Base
{
public:
int varD;
virtual void func(void){std::cout<<"Derived::func"<<std::endl;}
}; void func1(Base *pb)
{
Derived *pd1 = static_cast<Derived *>(pb);
std::cout<<"pd1 = "<<pd1<<std::endl;
pd1->func();
std::cout<<"varD = "<<pd1->varD<<std::endl; // 访问非法内存 Derived *pd2 = dynamic_cast<Derived *>(pb);
std::cout<<"pd2 = "<<pd2<<std::endl; // pd2 是空指针
pd2->func(); // 程序崩溃
} void func2(Derived *pd)
{
Derived *pd1 = static_cast<Derived *>(pd);
std::cout<<"pd1 = "<<pd1<<std::endl;
pd1->func();
std::cout<<"varD = "<<pd1->varD<<std::endl; Derived *pd2 = dynamic_cast<Derived *>(pd);
std::cout<<"pd2 = "<<pd2<<std::endl;
pd2->func();
} int main(void)
{
Base bs;
Base *pb = &bs; Derived dd;
Derived *pd = ⅆ func1(pb);
func2(pd); return ;
}
使用dynamic_cast时Base类中必须有虚函数,否则会编译出错;static_cast则没有这个限制。
2.2.3、const_cast
常量强制转换,用于强制转换 const或volatile 的数据,转换前后数据的类型必须相同,
主要用来在运算时暂时删除数据的const限制。
问题:http://bbs.csdn.net/topics/390741544
2.2.4、reinterpret_cast
重解释强制转换,是按照强制转换所指定的类型 对 被转换数据对应的内存空间 进行重新定义。
例如将int型和指针型之间的相互转换。
#include <iostream> int main(void)
{
int i = ;
char *pch = "Hello"; i = reinterpret_cast<int>(pch); std::cout<<reinterpret_cast<char *>(i)<<std::endl; return ;
} /*---- 输出结果---------
Hello
-----------------------*/
----------------------------------------------------------------------------------------------------------------
参考资料:
<<C++面向对象程序设计(第二版)>>
http://baike.baidu.com/link?url=t2_gReiazbNzDC506qpVVY9ynuOIeEwsQgBZ0A2uTrogCujubSr-Nb93A_xRpAa4R43G0HjRnNAu1M_ohqgzMa
C/C++类型转换的更多相关文章
- 为C# as 类型转换及Assembly.LoadFrom埋坑!
背景: 不久前,我发布了一个调试工具:发布:.NET开发人员必备的可视化调试工具(你值的拥有) 效果是这样的: 之后,有小部分用户反映,工具用不了(没反应或有异常)~~~ 然后,建议小部分用户换个电脑 ...
- c# 基础 object ,new操作符,类型转换
参考页面: http://www.yuanjiaocheng.net/webapi/config-webapi.html http://www.yuanjiaocheng.net/webapi/web ...
- Struts2日期类型转换
针对日期类java.util.Date进行类型转换,要求客户端使用"yyyy-MM-dd","yyyy/MM/dd"中的任意一种输入,并以"yyyy- ...
- 【.NET深呼吸】基础:自定义类型转换
照例,老周在开始吹牛之前,先讲讲小故事,这是朋友提出的建议,老TMD写技术有什么了不起的,人人都会写.后来老周想想,也确实,代码谁不会写,能写到有品位有感悟,就不容易做到.于是,老周接受了该朋友的建议 ...
- C++四种类型转换方式。
类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可以在任意类型之间转换,比 ...
- struts2类型转换
1. Struts2中的类型转换 我们知道通过HTTP提交到后台的数据,都是字符串的形式,而我们需要的数据类型当然不只字符串类型一种.所以,我们需要类型转换! 在Struts2中,类型转换的概念除了用 ...
- C++_系列自学课程_第_11_课_类型转换_《C++ Primer 第四版》
上次说了关于表达式的一些内容,说到还有一些关于数据类型转换的内容,今天我们接着八一八C++中的数据类型转换. 一.隐式类型转换 在表达式中,有些操作符可以对多种类型的操作数进行操作, 例如 + 操作符 ...
- Struts2入门(三)——数据类型转换
一.前言 笔者一直觉得,学习一个知识点,你首先要明白,这东西是什么?有什么用?这样你才能了解.好了,不说废话. 1.1.类型转换为何存在?什么是类型转换? 在MVC框架中,都是属于表示层解决方案,都需 ...
- js条件判断时隐式类型转换
Javascript 中,数字 0 为假,非0 均为真 在条件判断运算 == 中的转换规则是这样的: 如果比较的两者中有布尔值(Boolean),会把 Boolean 先转换为对应的 Number,即 ...
- JavaScript中数据类型转换总结
JavaScript中数据类型转换总结 在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换. 1, 显式数据类型转换 a:转数字: 1)Number转换: 代码: var a = " ...
随机推荐
- Linux 编辑器
vim编辑器 vi作为Unix上的一个编辑器,一直广受欢迎.之后GUN将其移植到开源世界中,经过开发人员对其进行了改善,被称为vi improved,就是现在的vim.为了方便使用,几乎所有的Linu ...
- 46.谈谈SDRAM的作用
SDRAM这个至今还在用的存储器,虽然被后来的DDR取代,掌握好它还是很重要的.之前在调试时,确实费了好大劲,它的复杂性毋庸置疑,一般人要想弄懂他,得花1个月左右吧,至少我这么认为.话说回来,SDRA ...
- Android facebook-android-sdk-3.5.2 FQL
android平台下使用Facebook 3.5.2的SDK实践 步骤总结: 1.引用SDK 在Facebook官网下载最新版的SDK,解压. 打开Eclipse, 'File' -> 'Imp ...
- < java.lang >-- StringBuffer字符串缓冲区
构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符. 特点: 1:可以对字符串内容进行修改. 2:是一个容器. 3:是可变长度的. 4:缓冲区中可以存储任意类型的数据. 5:最终需要变成字符 ...
- 动态链接库知识点总结之三(如何以显示的方式加载DLL)
总结一下如何显示加载方式加载DLL, 首先,我们新建一个win32项目,选择dll,空项目,再添加一个源文件,一个模块定义文件(.def),具体如下图.(详细方法已经在前两篇文章中讲述,如有不懂,打开 ...
- Android -- 倒计时的实现
CountDownTimer CountDownTimer这个 ...
- iTween
http://u3d.as/content/pixelplacement/i-tween/1s9 download http://itween.pixelplacement.com/documenta ...
- 利用doScroll在IE浏览器里模仿DOMContentLoaded
稍微了解一点框架的事件绑定的都知道 window.onload 事件需要在页面所有内容(包括图片.flash.iframe等)加载完后,才执行,但往往我们更希望在 DOM 一加载完就执行脚本,而各大框 ...
- SQL Server表分区案例
--学习创建表分区脚本/*SQL SERVER 2005中以上版本,终于引入了表分区,就是说,当一个表里的数据很多时,可以将其分拆到多个的表里,大大提高了性能.下面举例子说明之*/ --------- ...
- ubuntu文件定时加密压缩
#!/bin/sh #zip.sh DATE=`date -d yesterday +"%Y%m%d"` #password PASS="123456" if ...