C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:
TYPE b = (TYPE)a。
C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。

const_cast,字面上理解就是去const或volatile属性。
static_cast,命名上理解是静态类型转换。如int转换成char。
dynamic_cast,命名上理解是动态类型转换。如子类和父类之间的多态类型转换。
reinterpret_cast,仅仅重新解释类型,但没有进行二进制的转换。
4种类型转换的格式,如:TYPE B = static_cast<TYPE>(a)。

const_cast
去掉类型的const或volatile属性。

struct SA {
int i;
};
const SA ra;
//ra.i = 10; //直接修改const类型,编译错误
SA &rb = const_cast<SA&>(ra);
rb.i =10;

Syntax

 
const_cast < new_type > ( expression )    
 

Returns a value of type new_type.

Explanation

Only the following conversions can be done with const_cast. In particular, only const_cast may be used to cast away (remove) constness or volatility.仅仅以下的转换可以用const_cast

1) Two possibly multilevel pointers to the same type may be converted between each other, regardless of cv-qualifiers at each level.
2) lvalue of any type T may be converted to a lvalue or rvalue reference to the same type T, more or less cv-qualified. Likewise, an rvalue may be converted to a more or less cv-qualified rvalue reference.
3) Same rules apply to possibly multilevel pointers to data members.
4) null pointer value may be converted to the null pointer value of new_type

As with all cast expressions, the result is:

  • an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
  • an xvalue if new_type is an rvalue reference to object type;
  • a prvalue otherwise.

Notes

Pointers to functions and pointers to member functions are not subject to const_cast

Even though const_cast may remove constness from any pointer or reference, using the resulting pointer or reference to write to an object that was declared const invokes undefined behavior.

转自:http://en.cppreference.com/w/cpp/language/const_cast

static_cast

类似于C风格的强制转换。无条件转换,静态类型转换。用于:
1. 基类和子类之间转换:其中子类指针转换成父类指针是安全的;但父类指针转换成子类指针是不安全的。(基类和子类之间的动态类型转换建议用dynamic_cast)
2. 基本数据类型转换。enum, struct, int, char, float等。static_cast不能进行无关类型(如非基类和子类)指针之间的转换。
3. 把空指针转换成目标类型的空指针。
4. 把任何类型的表达式转换成void类型。
5. static_cast不能去掉类型的const、volitale属性(用const_cast)。

1 int n =6;
2 double d = static_cast<double>(n); // 基本类型转换
3 int*pn =&n;
4 double*d = static_cast<double*>(&n) //无关类型指针转换,编译错误
5 void*p = static_cast<void*>(pn); //任意类型转换成void类型
dynamic_cast
有条件转换,动态类型转换,运行时类型安全检查(转换失败返回NULL):
1. 安全的基类和子类之间转换。
2. 必须要有虚函数。否则报错。
3. 相同基类不同子类之间的交叉转换。但结果是NULL。
class BaseClass {
public:
int m_iNum;
virtualvoid foo(){}; //基类必须有虚函数。保持多台特性才能使用dynamic_cast
}; class DerivedClass: public BaseClass {
public:
char*m_szName[];
void bar(){};
}; BaseClass* pb =new DerivedClass();
DerivedClass *pd1 = static_cast<DerivedClass *>(pb); //子类->父类,静态类型转换,正确但不推荐
DerivedClass *pd2 = dynamic_cast<DerivedClass *>(pb); //子类->父类,动态类型转换,正确 BaseClass* pb2 =new BaseClass();
DerivedClass *pd21 = static_cast<DerivedClass *>(pb2); //父类->子类,静态类型转换,危险!访问子类m_szName成员越界
DerivedClass *pd22 = dynamic_cast<DerivedClass *>(pb2); //父类->子类,动态类型转换,安全的。结果是NULL,cout<<pdd22是NULL

如果去掉基类的virtual报错:

error C2683: “dynamic_cast”:“BaseClass”不是多态类型。

reinterpret_cast
仅仅重新解释类型,但没有进行二进制的转换:
1. 转换的类型必须是一个指针、引用、算术类型、函数指针或者成员指针。
2. 在比特位级别上进行转换。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。但不能将非32bit的实例转成指针。
3. 最普通的用途就是在函数指针类型之间进行转换。
4. 很难保证移植性。

int doSomething(){return0;};
typedef void(*FuncPtr)(); //FuncPtr is 一个指向函数的指针,该函数没有参数,返回值类型为 void
FuncPtr funcPtrArray[]; //10个FuncPtrs指针的数组 让我们假设你希望(因为某些莫名其妙的原因)把一个指向下面函数的指针存入funcPtrArray数组: funcPtrArray[] =&doSomething;// 编译错误!类型不匹配,reinterpret_cast可以让编译器以你的方法去看待它们:funcPtrArray
funcPtrArray[] = reinterpret_cast<FuncPtr>(&doSomething); //不同函数指针类型之间进行转换

转自:http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html

深入:

http://blog.csdn.net/pizi0475/article/details/6286826

c++类型转换Type Cast)的更多相关文章

  1. 一个问题:关于类型转换Type Cast(汇编讲解 as 语法)

    问题如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34   ...

  2. 类型强转(type cast)

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

  3. MySQL数据类型转换函数CAST与CONVERT的用法

    MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值.两者具体的语法如下: 1.CAST(value as type) 就是CAST(xxx AS 类型) 2 ...

  4. mysql 的类型转换函数cast的用法

    CAST(expr   AS   type),   CONVERT(expr,type)   ,   CONVERT(expr   USING   transcoding_name) CAST()   ...

  5. MySQL类型转换 使用CAST将varchar转换成int类型排序

    --使用CAST将varchar转换成int类型排序 select distinct(zone_id) from guild_rank_info order by CAST(zone_id as SI ...

  6. C++ function pointer and type cast

    http://www.cprogramming.com/tutorial/function-pointers.html http://www.cplusplus.com/doc/tutorial/ty ...

  7. eslint报错: Unexpected any value in conditional. An explicit comparison or type cast is required

    原代码: record.modifiedTime? 修改后代码:typeof record.modifiedTime !== 'undefined' ?   (isAddType === true ? ...

  8. C++开发必看 四种强制类型转换的总结 [转]

    一.C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:     TYPE b = (TYPE)a 二.C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. co ...

  9. C++四种强制类型转换关键字

    C语言使用强制类型转换(Type Cast)很简单,不管什么类型的转换,形式都如下: TYPE b = (TYPE)a; c++提供了4种类型转换操作符来应对不同场合的应用. const_cast   ...

随机推荐

  1. php 5.3起弃用session_register

    最近下了dedecms V5.7时,在登陆后台时,用户名和密码也没错,就是跳转不走,进不了后台管理页面,追踪了好久才发现根目录/include/userlogin.class.php中289行左右的位 ...

  2. 让图片在DIV中垂直居中

    window.onload=function(){ var img = document.getElementById("imgdiv"); var div = document. ...

  3. SQL多行拼接为一行

    使用简单T-SQL,拼接一列多行为一行.按SQL SERVER的说法叫做自拼接(PS:区分自连接) 还有一种方法是for xml path的方式,感觉不实用. declare @Result varc ...

  4. bootstrap学习和使用的经验总结

    第一肯定是下载 然后就是目录介绍,因为bootstrap是个轻量级的框架,目录不是很多,所以很容易理解,主要有用的就是三个文件,bootstrap.js,bootstrap.css,bootstrap ...

  5. wp 修改 提高youtu 速度

    resolve = 后添加 |.googlevideo.com ,并修改 crlf_rules crlf_rules = /^https?:\/\/[^\/]+\.c\.youtube\.com\// ...

  6. 1 Yoga3 系统装机总结.

    1- Yoga 3 存在串口驱动不安装, 那么触摸屏不能用的情况, 打破了以往对触摸屏-"纯外设" 的设想, 与系统有关!!! 2- 系统安装总结: 1) BIOS中设置UEFI ...

  7. c#基础-----数据类型,转义字符,引用类型,类型转换

    数据类型,转义字符,引用类型,类型转换 百度一下

  8. wel

    欢迎来到mathant.com 这个网站是什么 这个网站是我搭建在阿里云vps上的个人网站.目前的用途是充当个人博客和云存储,当然它的功能不止如此.我会在以后的日子里完善他,希望他能变得更好.目前我在 ...

  9. cocos2dx arpg单机手游

    这只是一个DEMO. ARPG 单机手游, 个人DEMO. 支持剧情编辑, 支持气泡对话, 支持人物图像对话, 支持随时角色切换, 支持NPC跟随, 共同作战, 支持LUA扩展, 支持BUFF技能, ...

  10. js中的prototye

    前言 没事的时候写着js完,一般可能大家都知道这个属性吧,但是我还要说说,给一些不知道的人看看吧, 希望对你有帮助. 过程 以前在学c#的时候,老师最多用的就是Person这个类来开讲,我觉得是这个更 ...