C++ in the four types of conversion methods detailed analysis

  • 2020-04-02 01:47:36
  • OfStack

The specific conclusion is as follows:

(1) the reinterpret_cast
This function converts a pointer of one type to a pointer of another type.
This conversion does not require changing the format of the pointer variable's value (it does not change the value of the pointer variable), but simply reinterpreting the type of the pointer at compile time.
Reinterpret_cast can convert a pointer value to an integer, but cannot be used for conversion of non-pointer types.
Ex. :
// type conversion of primitive type Pointers
Double d = 9.2;
Double * pd = & d;
Int * PI = reinterpret_cast < Int * > (pd);   // is equivalent to int* PI = (int*)pd;

// type conversion of Pointers to unrelated classes
Class A, {}.
Class B {};
A* pa = new A;
B * pb = reinterpret_cast < B * > (pa);     // B* pb = (B*)pa;

// pointer to an integer
Long l = reinterpret_cast < long > (PI);     // equivalent to long l = (long) PI;

(2) const_cast
This function removes the constant property of the pointer variable and converts it to a normal variable of the corresponding pointer type. Conversely, you can convert an inordinate number of pointer variables to a constant pointer variable.
This conversion is a type change made during compilation.
Ex. :
Const int* pci = 0;
Int * pk = const_cast < Int * > (pci);   // equivalent to int* pk = (int*)pci;

Const A* pca = new A;
A * pa = const_cast < A * > (pca);         // equivalent to A* pa = (A*)pca;

For security reasons, const_cast cannot convert non-pointer constants to normal variables.

(3) the static_cast
This function is mainly used for converting between basic types and between types with inheritance relationships.
This transformation generally changes the internal representation of variables, so it makes little sense for static_cast to be applied to pointer casts.
Ex. :
// basic type conversion
Int I = 0;
Double d = static_cast < A double > (I);   // double d = (double) I;

// the object of the transformation inheritance class is the base class object
The class Base {};
Class Derived: public Base{};
Derived d;
Base b = static_cast < The Base > (d);         // equivalent to Base b = (Base)d;

(4) dynamic_cast
It is a dynamic transformation as opposed to static_cast.
This conversion is analyzed at run time, not at compile time, and is distinct from the above three type conversion operations.
This function can only cast between Pointers or references to inherited class objects. When converting, it determines whether the conversion between type objects is legal based on the current runtime type information. A pointer conversion to dynamic_cast fails, which can be checked for null, and a bad_cast exception is thrown when the reference conversion fails.
Ex. :
The class Base {};
Class Derived: public Base{};

// derived class pointer to base class pointer
Derived *pd = new Derived;
The Base * pb = dynamic_cast < The Base * > (pd);

If (! Pb)
      cout < < "Type conversion failed" < < Endl;

// there is no inheritance, but the transformed class has virtual functions
Class A (virtual ~ (A))     // has virtual functions
Class {B} :
A* pa = new A;
B * pb   = dynamic_cast < B * > (pa);

Object Pointers without inheritance or virtual functions, Pointers to primitive types, and Pointers to derived classes cannot be converted.


Related articles: