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 = " ...
随机推荐
- 条款7:为多态基类声明virtual析构函数
C++明确指出:当派生类对象是由一个基类指针释放的,而基类中的析构函数不是虚函数,那么结果是未定义的.其实我们执行时其结果就是:只调用最上层基类的析构函数,派生类及其中间基类的析构函数得不到调用. # ...
- android开发 WriteUTF与readUTF 原理
今晚上写代码玩,用到java.io.RandomAccessFile.writeUTF(String)函数,而文件默认保存为gbk,显然是乱码.突然想起来去看看存储编码规则,就去找了些文章了解writ ...
- Portion of class Throwable’s inheritance hierarchy
- cookie和session的代码实现
cookie和session的代码实现 1.设置cookie 今天笔试题考的是cookie的设置,我竟然选了request也可以设置cookie,我的天呀. 我们来看如何在response设置吧 pu ...
- git的初步了解
其实git我也不熟,请两天假回来宿友告诉我,我们有一份高大尚的作业.我问她们才知道原来是让我们以博客的形式写两份作业交上去.git还是我在网上查找到的,才对它有一些的了解. git是一个开源分布式版本 ...
- hdu 2255 奔小康赚大钱 最大权匹配KM
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事 ...
- if in hlsl
seems that in HLSL_4, we can use if https://msdn.microsoft.com/en-us/library/bb313972(v=xnagamestudi ...
- android中PreferenceScreen类的用法
PreferenceScreen preference是偏好,首选的意思,PreferenceScreen个人翻译成 “偏好显示”,明白这个意思就好,就是说根据特点灵活的定义显示内容风格,一个屏幕可以 ...
- Virtualbox网络设置和无UI启动
因工作需要,在Macbook上安装Ubuntu 14.04.2虚拟机,需要ssh连接操作. 一番查找资料,实践后可以正常工作了,记录一些信息以备用 无UI启动虚拟机,可使用以下命令: VBoxMana ...
- YARN应用程序的开发步骤
开发基于YARN的应用程序需要开发客户端程序和AppMaster程序: 我们基于程序自带的例子来实现提交application 到YARN的ResourceManger. Distributed Sh ...