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 = " ...
随机推荐
- [转]HTTPS那些事(一)HTTPS原理
[转]HTTPS那些事(一)HTTPS原理 http://www.guokr.com/post/114121/ 楔子谣言粉碎机前些日子发布的<用公共WiFi上网会危害银行账户安全吗?>, ...
- C#取枚举描述
一直都觉得枚举是个很不错的东西,可以给我们带来很多方便,而且也增加代码的可读性. 我在之前已经介绍过枚举的简要应用了,再次再来写下怎么获取枚举的描述. 源码如下: 首先,我们定义个含有描述的枚举类型 ...
- 关于spring mvc MaxUploadSizeExceededException 死循环解决方案
当看到这文章的时候相信你现在应该遇到这样的问题了,我也是自己遇到了后来找到解决方案了记录下来,如果下次遇到就可以直接解决了. 至于为什么会出现这样的情况,可以看这篇文章:https://bz.apac ...
- 微软职位内部推荐-Software Engineer II-Data Mini
微软近期Open的职位: Are you looking for a big challenge? Do you know why Big Data is the next frontier for ...
- 20145103 《Java程序设计》第3周学习总结
20145103 <Java程序设计>第3周学习总结 教材学习内容总结 第四章我首先了解了CPU与内存的关系,栈与堆的关系.要产生对象必须先定义类,类是对象的设计图,对象是累的实例.以类名 ...
- NABC的特点分析
题目: 请把采用卡片分类的方法讨论你们的团队开发项目特点,再按照 NABC 的框架分析每个特点. 每一个组员针对其中的一个特点将NABC的分析结果发表博 ...
- 阿里云服务器Node环境配置
最近,将网站的阿里云服务器迁移到阿里云北京机房,记录下CentOS的迁移过程. 首次登录云服务器,要先进行用户设置. 用户设置 首先用passwd命令修改超级管理员root密码. $ passwd 根 ...
- 第七周技术博客发表 web网页开发
a<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org ...
- 省选加油>_<
今天没有写题诶……看了看以前的模板……明天就要省选了>_<加油~~ 要不再去打局dota吧>_>
- 【CodeForces】【311E】Biologist
网络流/最大权闭合图 题目:http://codeforces.com/problemset/problem/311/E 嗯这是最大权闭合图中很棒的一道题了- 能够1A真是开心-也是我A掉的第一道E题 ...