C++四种强制转换

reinterpret_cast

/*  
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes.  
The operation result is a simple binary copy of the value from one pointer to the other.  
All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.  
It can also cast pointers to or from integer types.   
The format in which this integer value represents a pointer is platform-specific.   
The only guarantee is that a pointer cast to an integer type large enough to fully contain it (such as intptr_t),  
is guaranteed to be able to be cast back to a valid pointer.  
reinterpret可用于任何指针向任何指针的转换。它只是简单的进行二进制拷贝。  
它还可以用于将指针类型和整型类型相互转换(注意整型类型和指针类型的长度不一致)。  
它不进行类型检查。  
*/ 

reinterpret_cast转换是在类C转换的基础上,在编译期间
约束了整型、浮点型和枚举类型的相互转换。
static_cast

/*  
static_cast can perform conversions between pointers to related classes,   
not only upcasts (from pointer-to-derived to pointer-to-base),  
but also downcasts (from pointer-to-base to pointer-to-derived).   
No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type.   
Therefore, it is up to the programmer to ensure that the conversion is safe.   
On the other side, it does not incur the overhead of the type-safety checks of dynamic_cast.  
它用于在存在继承关系的类指针之间转换。可以从派生类指针转为基类指针,也可以从基类指针转为派生类指针。  

static_cast is also able to perform all conversions allowed implicitly  
(not only those with pointers to classes), and is also able to perform the opposite of these.   
It can:  
Convert from void* to any pointer type. In this case,   
it guarantees that if the void* value was obtained by converting from that same pointer type, the resulting pointer value is the same.  
Convert integers, floating-point values and enum types to enum types.  
它可以将void*型向任意指针类型转换。还可以在整型、浮点型和枚举型将相互转换。  
*/ 

static_cast
约束了指针和整型的相互转换。
约束了无关系类型的指针的相互转换。(无类型指针除外)
dynamic_cast

/*  
dynamic_cast can only be used with pointers and references to classes (or with void*).   
Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type.  
This naturally includes pointer upcast (converting from pointer-to-derived to pointer-to-base), in the same way as allowed as an implicit conversion.  
But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members)  
if -and only if- the pointed object is a valid complete object of the target type.  

dynamic_cast can also perform the other implicit casts allowed on pointers: casting null pointers between pointers types (even between unrelated classes),  
and casting any pointer of any type to a void* pointer.  
dynamic_cast只可以用于指针之间的转换,它还可以将任何类型指针转为无类型指针,甚至可以在两个无关系的类指针之间转换。  
*/ 

(主要的功能是提供向下转换时的安全检测。子类转换为父类是安全的,反之则未必)
将一个基类对象指针(或引用)cast到继承类指针,dynamic_cast会根据基类指针是否真正指向继承类指针来做相应处理,若是转换失败,dynamic_cast将会对这次操作返回Null。
如果转换目标是引用类型并且失败了,dynamic_cast将抛出一个bad_cast异常

一般来说,因为RTTI技术作用于运行时,所以其会产生运行时的代价。所以很多人建议,如果在能明确转换安全的场景下,不要使用dynamic_cast方法进行转换,而是使用static_cast,以免进行一些不必要的运行时计算。
const_cast
const_cast转换符是用来移除变量的const或volatile限定符
我们可能调用了一个参数不是const的函数,而我们要传进去的实际参数确实const的,但是我们知道这个函数是不会对参数做修改的。于是我们就需要使用const_cast去除const限定,以便函数能够接受这个实际参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值