C + + provides four cast operators:static_cast,const_cast,dynamic_cast and reinterpret_cast.
1. static_cast
2. const_cast
- A constant class object (or primitive type) is not supported for conversion to a non-Const class object (or base type).
- The constant pointer is converted to a very variable pointer, and the converted pointer points to the original.
- A constant reference is converted to a very literal reference.
3. dynamic_cast
- For inheritance relationships, only class object pointers and referenced class conversions are supported.
- Casts a base class object pointer (or reference) to an inherited class pointer and reference, and dynamic_cast handles it according to whether the translated pointer or reference really points to an inherited class pointer or reference. Conversion between pointers fails, returns NULL, reference conversion fails, throws an exception.
- The downstream conversion of a pointer or reference is premised on a virtual function of the base class.
4. reinterpret_cast
- does not have portability.
- Used primarily to convert function pointers.
Clause 2: It is best to use C + + transformation operators