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 = " ...
随机推荐
- 如何配置DNS服务器(局域网——域名指向某个IP地址)
单击“开始”,指向“管理工具”,然后单击“DNS”,打开 DNS 管理器. 如有必要,向管理单元添加适用的服务器,然后连接该服务器.在控制台树中,单击适用的 DNS 服务器. 在“操作”菜单上 ...
- 【Masonry】使用技巧 - 篇一
从别人项目得到的灵感 : 请看以下代码 UIColor *darkColor = [UIColor colorWithHexString:@"0x28303b"]; // 1. 确 ...
- iOS9新系统下APP Store 应用上传新指南
一 iTunes Connect介绍 iTunes Connect是面向iOS应用开发人员的苹果门户网站,供开发人员管理其应用,跟踪下载情况.今年1月份闹得沸沸扬扬的iTunes Connect BU ...
- html表格属性
一.在表格中插入文字及图片 1.把图片及文字分开到不同的[tr]标签表格内. <html> <body> <table border="1" widt ...
- 20145129 《Java程序设计》第8周学习总结
20145129 <Java程序设计>第8周学习总结 教材学习内容总结 NIO NIO使用频道(channel)来衔接数据节点,对数据区的标记提供了clear(),rewind(),fli ...
- 团队开发之《极速蜗牛》NABC分析
一.简介 项目名称:极速蜗牛 特点:操作简单,视觉与听觉配合,让用户有最完美的体验. 二.NABC分析 N(need):在人们无时无刻离不开手机的今天,难免有无聊的时候,此刻一款操作简单又能令人们动脑 ...
- Struts2原码分析系列之一
struts2概述 在struts2的官网上有这么一句话,翻译为:Apache Struts2是一个为企业级应用打造的优秀的.可扩展的WEB框架,该框架旨在充分精简应用程序的开发周期,从而减少创建.发 ...
- Bootstrap入门二:响应式页面布局
Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列.它包含了易于使用的预定义类,还有强大的mixin 用于生成更具 ...
- Careercup - Microsoft面试题 - 6543214668414976
2014-05-11 02:56 题目链接 原题: Write a function called FooBar that takes input integer n and prints all t ...
- Analyzer的报表复制、移动
制作Analyzer报表后,希望可以直接拷贝到其他机子上(无法通过网络连接到) 方法很简单: 1.进入Analyzer的数据库服务器设定页面,查找到连接的系统数据库是哪个 2.将该系统数据库备份出来 ...