C++'s 4 type conversion keywords and their characteristics

All contents of this article are from http://blog.sina.com.cn/s/blog_4b20940201013ryp.html , thanks to this author :)


reinterpret_cast

'reinterpret_cast' converts a pointer to another type of pointer. It also allows conversion from a pointer to an integer type. vice versa. (Annotation: Is the specific address value of the pointer as an integer value?)
This operator can convert between unrelated types. The result of the operation is simply a binary copy of the value from one pointer to another. The content pointed to between types does not do any type checking and conversion.

If the situation is a copy from a pointer to an integer, the interpretation of the content is system-dependent, so any implementation is not convenient. A pointer converted to a sufficiently large integer type to contain it can be converted back to a valid pointer.

Code:
class A {};
class B {};

A * a = new A;
B * b = reinterpret_cast<B *>(a);
'reinterpret_cast' treats all pointer type conversions like traditional type conversions.

static_cast

'static_cast' allows any implicit conversion and reverse conversion actions to be performed. (Even if it is not allowed implicit)

Applied to the pointer of the class, it means that it allows the pointer of the subclass type to be converted to the pointer of the parent type (this is a valid implicit conversion), and at the same time, it can also perform the opposite action: convert the parent class to its child class.

In this last example, the converted parent class is not checked for consistency with the destination type.
Code:
class Base {};
class Derived: public Base {};

Base *a = new Base;
Derived *b = static_cast<Derived *>(a);
In addition to manipulating type pointers,'static_cast' can also be used to perform type-defined explicit conversions and standard conversions between basic types:

代码:
double d = 3.14159265;
int i = static_cast<int>(d);

dynamic_cast

'dynamic_cast' is only used for pointers and references of objects. When used in polymorphic types, it allows arbitrary implicit type conversions and the reverse process. However, unlike static_cast, in the latter case (note: the reverse process of implicit conversion), dynamic_cast checks whether the operation is valid. That is, it checks whether the conversion will return a valid complete object that was requested.
Detection is performed at runtime. If the converted pointer is not a valid and complete object pointer requested, the return value is NULL.
Code:
class Base {virtual dummy() {} };
class Derived: public Base {};

Base* b1 = new Derived;
Base* b2 = new Base;

Derived* d1 = dynamic_cast<Derived *>(b1); // succeeds
Derived* d2 = dynamic_cast<Derived *>(b2); // fails: returns 'NULL'

如果一个引用类型执行了类型转换并且这个转换是不可能的,一个bad_cast的异常类型被抛出:
代码:
class Base { virtual dummy() {} };
class Derived : public Base { };

Base* b1 = new Derived;
Base* b2 = new Base;

Derived d1 = dynamic_cast<Derived &*>(b1); // succeeds
Derived d2 = dynamic_cast<Derived &*>(b2); // fails: exception thrown

const_cast

这个转换类型操纵传递对象的const属性,或者是设置或者是移除:
代码:
class C {};

const C *a = new C;

C *b = const_cast<C *>(a);
其它三种操作符是不能修改一个对象的常量性的。
注意:'const_cast'也能改变一个类型的volatile qualifier。

--------------------------------------------------------------------

C++的4种类型转换

一、C 风格(C-style)强制转型如下:

(T) expression // cast expression to be of type T
函数风格(Function-style)强制转型使用这样的语法:
T(expression) // cast expression to be of type T
这两种形式之间没有本质上的不同,它纯粹就是一个把括号放在哪的问题。我把这两种形式称为旧风格(old-style)的强制转型。

 

 

二、 C++的四种强制转型形式:

  C++ 同时提供了四种新的强制转型形式(通常称为新风格的或 C++ 风格的强制转型):
  const_cast(expression)
  dynamic_cast(expression)
  reinterpret_cast(expression)
  static_cast(expression)

  每一种适用于特定的目的:

  ·dynamic_cast 主要用于执行“安全的向下转型(safe downcasting)”,也就是说,要确定一个对象是否是一个继承体系中的一个特定类型。它是唯一不能用旧风格语法执行的强制转型,也是唯一可能有重大运行时代价的强制转型。

·static_cast 可以被用于强制隐型转换(例如,non-const 对象转型为 const 对象,int 转型为 double,等等),它还可以用于很多这样的转换的反向转换(例如,void* 指针转型为有类型指针,基类指针转型为派生类指针),但是它不能将一个 const 对象转型为 non-const 对象(只有 const_cast 能做到),它最接近于C-style的转换。

  ·const_cast 一般用于强制消除对象的常量性。它是唯一能做到这一点的 C++ 风格的强制转型。

  ·reinterpret_cast 是特意用于底层的强制转型,导致实现依赖(implementation-dependent)(就是说,不可移植)的结果,例如,将一个指针转型为一个整数。这样的强制转型在底层代码以外应该极为罕见。
  
  旧风格的强制转型依然合法,但是新的形式更可取。首先,在代码中它们更容易识别(无论是人还是像 grep 这样的工具都是如此),这样就简化了在代码中寻找类型系统被破坏的地方的过程。第二,更精确地指定每一个强制转型的目的,使得编译器诊断使用错误成为可能。例如,如果你试图使用一个 const_cast 以外的新风格强制转型来消除常量性,你的代码将无法编译。

Guess you like

Origin blog.csdn.net/youarenotme/article/details/54916871