There are C-style conversions and, of course, C ++ style conversions. The conversion format of C language is very simple (TYPE EXPRESSION), but the TYPE conversion of C language has many disadvantages. Sometimes it is not appropriate to use C because it can convert between any type, for example:

  1. You can convert a pointer to a const object to a pointer to a nonconst object, and a pointer to a base object to a pointer to a derived object. The difference between these two conversions is huge, but c does not make that distinction.
  2. Another drawback is that C conversions are not easy to find. They consist of a parenthesis and an identifier, and c++ programs are full of such things. To overcome these shortcomings, c++ introduced four new type conversion operators, as follows:

    1. static_cast
    2. const_cast
    3. dynamic_cast
    4. reinterpret_cast.

1.static_cast

Similar to C language cast. Unconditional conversion, static type conversion. 1. Conversion between base and subclass: it is safe to convert a subclass pointer to a parent class pointer; However, it is not safe to convert a parent pointer to a child pointer. Dynamic_cast is recommended for dynamic type conversions between base classes and subclasses. Basic data type conversion. Enum, struct,int, char, float etc. Static_cast cannot be converted between Pointers to irrelevant types, such as non-base and subclasses. 3. Convert a null pointer to a null pointer of the target type. 4. Convert any type of expression to void. 5. Static_cast cannot remove const, volitale attributes of the type (const_cast).

int n = 6; double d =static_castdouble>(n); Int *pn = &n; Double *d =static_castdouble *>(&n) void *p =static_castvoid *>(pn); // Any type is converted to voidCopy the code

2.const_cast

Use to fetch const attributes, remove const or volatile attributes, and make Pointers to const to nonconst, as in:

Const int* fun(int x,int y){} int* PTR =const_cast<int *>(fun(2.3))Copy the code

3.dynamic_cast

This operator is used at run time to check whether the conversion is type safe, but is only valid for polymorphic types, that is, the class has at least one virtual method. Dynamic_cast has the same basic syntax as static_cast. Dynamic_cast is mainly used for up and down conversions between class hierarchies, and can also be used for cross conversions between classes. Dynamic_cast has the same effect as static_cast when converting upstream between class hierarchies; Dynamic_cast is more secure than Static_cast because it has type checking for downlink conversions. Such as:

Class C {/ /... C no virtual function}; The class T {/ /... } intmain() { dynamic_cast<T*> (new C); Class C {public: virtual void} class C {public: virtual voidm() {}; // C is now polymorphic}Copy the code

4.reinterpret_cast

Interpret means to interpret, and reinterpret means to reinterpret. This identifier means to reinterpret the binary form of the data without changing its value. Such as:

int i; 
char *ptr = "hello freind!"; i = reinterpret_cast<int>(ptr); // This conversion is rarely used.Copy the code