Casting Operators

There are several casting operators specific to the C++ language. These operators are intended to remove some of the ambiguity and danger inherent in old style C language casts. These operators are:

  • dynamic_cast: Used for conversion of polymorphic types.
  • static_cast:  Used for conversion of nonpolymorphic types.
  • const_cast:  Used to remove the const, volatile, and __unaligned attributes.
  • reinterpret_cast:  Used for simple reinterpretation of bits.
  • safe_cast:   Used to produce verifiable MSIL.

 

dynamic_cast: Used for conversion of polymorphic types.

static_cast:  Used for conversion of nonpolymorphic types.
In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead.
执行非多态的类型转换,用于代替C中通常的转换操作。
static_cast 在编译时使用类型信息执行转换,在转换执行必要的检测(诸如指针越界计算, 类型检查), 其操作相对是安全的。下面是一个例子:

int  n = 9 double  d = static_cast  <   double   >  (n); 

上面的例子将一个变量从 int 转换到 double。 这些类型的二进制表达式是不同的。 要将整数 9 转换到 双精度整数 9,static_cast 需要正确地为双精度整数 d 补足比特位。其结果为 9.0。另外一个例子:

static_cast

 

In contrast to dynamic_cast, no run-time check is made on the static_cast conversion of pb. The object pointed to by pb may not be an object of type D, in which case the use of *pd2 could be disastrous. For instance, calling a function that is a member of the D class, but not the B class, could result in an access violation.

The dynamic_cast and static_cast operators move a pointer throughout a class hierarchy. However, static_cast relies exclusively on the information provided in the cast statement and can therefore be unsafe. For example:

static_cast

If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.

If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer's assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.

Consequently, static_cast can do the inverse of implicit conversions, in which case the results are undefined. It is left to the programmer to verify that the results of a static_cast conversion are safe.

This behavior also applies to types other than class types. For instance, static_cast can be used to convert from an int to a char. However, the resulting char may not have enough bits to hold the entire int value. Again, it is left to the programmer to verify that the results of a static_cast conversion are safe.

The static_cast operator can also be used to perform any implicit conversion, including standard conversions and user-defined conversions. For example:

 

static_cast


Remarks:
1. The static_cast operator can explicitly convert an integral value to an enumeration type. If the value of the integral type does not fall within the range of enumeration values, the resulting enumeration value is undefined.

2. The static_cast operator converts a null pointer value to the null pointer value of the destination type.

3. Any expression can be explicitly converted to type void by the static_cast operator. The destination void type can optionally include the const, volatile, or __unaligned attribute.The static_cast operator cannot cast away the const, volatile, or __unaligned attributes.

4. Due to the danger of performing unchecked casts on top of a relocating garbage collector, the use of static_cast should only be in performance-critical code when you are certain it will work correctly. If you must use static_cast in release mode, substitute it with safe_cast in your debug builds to ensure success.


const_cast
:  Used to remove the const, volatile, and __unaligned attributes.
const_cast将指针或者引用显式转换成不具有const,volatile和__unaligned属性的另一个指针或者引用。
For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior.
Remarks:

1. You cannot use the const_cast operator to directly override a constant variable's constant status.

2. The const_cast operator converts a null pointer value to the null pointer value of the destination type. 

reinterpret_cast:  Used for simple reinterpretation of bits.
重新解释类型转换只是重新解释了给出的对象的bits,而没有进行二进制转换。

 

int  n = 9 ;   double  d = reinterpret_cast < double   &   >  (n);  

这次, 结果有所不同. 在进行计算以后, d 包含无用值. 这是因为 reinterpret_cast 仅仅是复制 n 的比特位到 d, 没有进行必要的分析。在VS2008上使用cout输出d的结果是-9.2559592117432096e+061,这跟9已经完全没有关系了。
下面是MSDN上的另外一个例子:

reinterpret_cast

这个例子使用reinterpret_cast将指针转换为整形,达到Hash的效果。 
Remarks:
1. The result of a reinterpret_cast cannot safely be used for anything other than being cast back to its original type. Other uses are, at best, nonportable.
2. The reinterpret_cast operator cannot cast away the const, volatile, or __unaligned attributes.
3. The reinterpret_cast operator converts a null pointer value to the null pointer value of the destination type.

safe_cast:   Used to produce verifiable MSIL.

The compiler will accept a static_cast in most places that it will accept a safe_cast. However, safe_cast is guaranteed to produce verifiable MSIL, where as a static_cast could produce unverifiable MSIL.
Remarks:
1. safe_cast invokes user-defined conversions.
2. safe_cast is a keyword defined inside the cli namespace, which is a compiler-defined namespace.
3. safe_cast does not apply a const_cast (cast away const).

safe_cast是一个编译时keyword,它在编译时会根据编译时全局类型信息,为cast操作生成verifiable的MSIL。

 

你可能感兴趣的:(Opera)