Implementation of xxx_cast type conversion

  • 2020-05-10 18:33:22
  • OfStack

xxx_cast is a general term that refers to static_cast (static conversion),const_cast (constant conversion),reinterpert_cast (reinterpreted conversion), dynamic_cast (dynamic conversion). This time we will learn about its use and conversion mode.

1.static_cast (static conversion)

Static transformations are all used to explicitly define conversions to achieve, including safe transformations that the compiler allows us to do without casting and less secure but clearly defined transformations.

2.const_cast (constant conversion)

If you convert from const to non-const or from volatile to non-volatile, you can use const_cast directly, which is the only conversion allowed by 1.

3.reinterpert_cast (reinterpreted conversion)

This is one of the most insecure conversion mechanisms and is most likely to cause problems. reinterpret_cast assumes an object as a schema, which is an object of a completely different type. reinterpret_cast is actually always required to convert reinterpret_cast back to its original type before doing anything with reinterpret_cast. reinterpret_cast in C++ is mainly used to transposition data from one type to another. By "providing a lower level of reinterpretation for the bit patterns of operands in general," I mean the reinterpretation of data in a binary format.

4.dynamic_cast (dynamic conversion)

In the inheritance system, it is mainly used for down conversion of type safety.


Related articles: