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 = &dd; func1(pb);
func2(pd); return ;
}

使用dynamic_cast时Base类中必须有虚函数,否则会编译出错;static_cast则没有这个限制。

Base中需要检测有虚函数的原因:由于运行时类型检查需要运行时类型信息,而这个信息存储在类的虚函数表中。
类中存在虚函数,就说明它有想要让基类指针或引用指向派生类对象的情况,此时转换才有意义。 

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++类型转换的更多相关文章

  1. 为C# as 类型转换及Assembly.LoadFrom埋坑!

    背景: 不久前,我发布了一个调试工具:发布:.NET开发人员必备的可视化调试工具(你值的拥有) 效果是这样的: 之后,有小部分用户反映,工具用不了(没反应或有异常)~~~ 然后,建议小部分用户换个电脑 ...

  2. c# 基础 object ,new操作符,类型转换

    参考页面: http://www.yuanjiaocheng.net/webapi/config-webapi.html http://www.yuanjiaocheng.net/webapi/web ...

  3. Struts2日期类型转换

    针对日期类java.util.Date进行类型转换,要求客户端使用"yyyy-MM-dd","yyyy/MM/dd"中的任意一种输入,并以"yyyy- ...

  4. 【.NET深呼吸】基础:自定义类型转换

    照例,老周在开始吹牛之前,先讲讲小故事,这是朋友提出的建议,老TMD写技术有什么了不起的,人人都会写.后来老周想想,也确实,代码谁不会写,能写到有品位有感悟,就不容易做到.于是,老周接受了该朋友的建议 ...

  5. C++四种类型转换方式。

    类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可以在任意类型之间转换,比 ...

  6. struts2类型转换

    1. Struts2中的类型转换 我们知道通过HTTP提交到后台的数据,都是字符串的形式,而我们需要的数据类型当然不只字符串类型一种.所以,我们需要类型转换! 在Struts2中,类型转换的概念除了用 ...

  7. C++_系列自学课程_第_11_课_类型转换_《C++ Primer 第四版》

    上次说了关于表达式的一些内容,说到还有一些关于数据类型转换的内容,今天我们接着八一八C++中的数据类型转换. 一.隐式类型转换 在表达式中,有些操作符可以对多种类型的操作数进行操作, 例如 + 操作符 ...

  8. Struts2入门(三)——数据类型转换

    一.前言 笔者一直觉得,学习一个知识点,你首先要明白,这东西是什么?有什么用?这样你才能了解.好了,不说废话. 1.1.类型转换为何存在?什么是类型转换? 在MVC框架中,都是属于表示层解决方案,都需 ...

  9. js条件判断时隐式类型转换

    Javascript 中,数字 0 为假,非0 均为真 在条件判断运算 == 中的转换规则是这样的: 如果比较的两者中有布尔值(Boolean),会把 Boolean 先转换为对应的 Number,即 ...

  10. JavaScript中数据类型转换总结

    JavaScript中数据类型转换总结 在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换. 1, 显式数据类型转换 a:转数字: 1)Number转换: 代码: var a = " ...

随机推荐

  1. [转]IP_ADD_MEMBERSHIP : 组播错误 的解决方法:

    [转]IP_ADD_MEMBERSHIP : 组播错误 的解决方法: http://www.cnitblog.com/dvb-dvb/archive/2012/10/15/aa.html by def ...

  2. H5 input type="search" 不显示搜索 解决方法

    在IOS(ipad iPhone等)系统的浏览器里打开H5页面.如下写法: <input type="search" name="search” id=" ...

  3. 如何在Quartus II中设置Virtual pin

    为了验证FPGA工程中的某个模块的功能和时序的正确性,常常需要对其单独进行验证,但是这些模块通常都与内部的众多信号相连(如系统总线,中断信号线等),往往一个模块的对外接口引脚会多达几百个,对其单独仿真 ...

  4. 命令学习:iftop

    iftop显示带宽使用情况 http://riobard.com/2010/04/30/l2tp-over-ipsec-ubuntu/ http://jaseywang.me/2011/12/19/i ...

  5. 安装配置Apache2.4和php7.0

    接下来就要进入到PHP的学习了,所以要安装Apache服务器和PHP,从昨天开始一直到刚刚才配置完成,中间也遇到了一些问题,所以整理一下写了下来.接下来就是Win64位系统配置Apache2.4和PH ...

  6. AJAX三种返回值方式

    (一)TEXT方式 该方式返回的是拼接字符串,想要取到其中的值,需要先将返回值进行拆分 (二)JSON方式 该方式返回的是数组,想要取到其中的值,可用索引项进行提取 (三)XML方式 XML:可扩展标 ...

  7. 1564: [NOI2009]二叉查找树 - BZOJ

    Description Input Output只有一个数字,即你所能得到的整棵树的访问代价与额外修改代价之和的最小值.Sample Input4 101 2 3 41 2 3 41 2 3 4Sam ...

  8. 【python】文件的输入和输出

    1.os模块 2.os.path 模块 3.实例 1. os模块 对文件系统的访问大多通过python的os模块实现,其中os 模块负责大部分的文件系统操作,包括删除/重命名文件,遍历目录树,管理文件 ...

  9. iNode for linux install

    http://wenku.baidu.com/link?url=953T6GZCnaBzwr4YqPFUT4oOyYr4wyOnXlCLO1OUYZkaJWh2fTs634SM7ZpYiTKkpmYX ...

  10. WP手机升级WIN10被PIN码锁定

    WP8.1手机升级WIN10后,需要输入PIN码(不知道啊),多次输入(1234,0000,8888 ...)后被锁定,无法使用手机(郁闷), 重启无数次,提示由于多次输入PIN码,手机无法使用(天啊 ...