1.Static_cast
The most common type conversion operator, which is a type conversion under normal conditions, such as converting int to float, such as: int I; float F; F = (float) I; or F = static_cast <float> (I );
· Static _ cast can be used to force implicit conversions (for example, converting a non-const object to a const object, or converting an int to a double object ), it can also be used for many reverse conversions (for example, void * pointer transformation to type pointer, base class pointer transformation to derived class pointer ), however, it cannot transform a const object to a non-const object (only const_cast can do it), and it is closest to the C-style conversion.
2.Const_cast
Used to retrieve the const attribute and change the const type pointer to a non-const type pointer, for example, const int * Fun (int x, int y) {} int * PTR = const_cast <int *> (fun (2.3 ))
3.Dynamic_cast
That is, dynamic_cast can be used for the downward transformation in the inheritance system, which converts the base class pointer to the derived class pointer, Which is stricter and safer than static_cast. Dynamic_cast is less efficient than static_cast in execution, but static_cast can complete ing in a wider range. This unrestricted ing is accompanied by insecurity.
4.Reinterpret_cast
Interpret indicates the meaning of interpretation. reinterpret indicates a re-interpretation. This identifier indicates a re-interpretation of the data in binary format, but does not change its value. For example: int I; char * PTR = "Hello freind! "; I = reinterpret_cast <int> (PTR); this conversion method is rarely used.
Int n = 9; double D = static_cast <double> (N );
In the above example, we convert a variable from int to double. these types of binary expressions are different. to convert an integer 9 to a double-precision integer 9, static_cast must complement the bitwise of the double-precision integer d correctly. the result is 9.0. the reinterpret_cast behavior is different:
Four types of conversion