Type conversion (Casting)

With static_cast<>()other C ++ type conversion, do not use int y = (int) x; or int int Y = (X); .

Definitions: C ++ is different from the introduction of different types of C type conversion operation.

Advantages: C language type conversion operation that vague: sometimes do cast (e.g., (int) 3.5 of ), the type of conversion is sometimes done (e.g., (int) "Hello" ). In addition, C ++ casts to find easier and more eye-catching.

Disadvantages: syntax is disgusting.

Conclusion: The use of C ++ rather than C-style style type conversion.

  1. static_cast: C-style conversion and similar cast do value, or a pointer to a parent class clear upconverting subclass;
  2. const_cast: Remove constproperty;
  3. reinterpret_cast: Between pointer and integer types or other unsafe pointer interchangeable, when do you use for everything clear in mind only;
  4. dynamic_cast: Do not use addition to the test, in addition to unit testing, if you need to type information determined at runtime, illustrate the design is flawed.

Guess you like

Origin blog.csdn.net/weixin_34255055/article/details/91030165