Skip to content

Latest commit

 

History

History
9 lines (5 loc) · 1.91 KB

File metadata and controls

9 lines (5 loc) · 1.91 KB

Explicit casting allows programmers to convert a data type into another data type. There are four kinds of explicit type casts: static_cast, reinterpret_cast, const_cast, and dynamic_cast.

  1. static_cast: This is the most common cast, and can be used in any scenario where implicit conversion is possible, as well as for additional operations. This includes numeric conversions, conversions between a pointer to a base and a pointer to a derived class, and more. However, it does not perform any runtime checks, so improper use can lead to errors. It's generally safer than implicit conversions because it makes the programmer's intention explicit.

  2. reinterpret_cast: This is a powerful cast that can basically cast any pointer or integral type to any other pointer or integral type. It's often used for low-level operations, like converting between function pointers and void*, or for bitwise copying of one type to another. However, it's very risky to use because it can easily lead to misinterpreted data or undefined behavior.

  3. const_cast: This is used to add or remove the const, volatile, or const volatile qualification from a variable. This is the only C++ style cast that can do this. It's generally used when you need to pass a const variable to a function that takes a non-const argument but you know the function won't actually modify the argument. It should be used sparingly, as misuse can easily lead to undefined behavior.

  4. dynamic_cast: This cast is used for safe downcasting (from a base pointer/reference to a derived pointer/reference). It includes a runtime check to ensure that the object being cast is actually of the target derived type, and if not, it returns nullptr (for pointers) or throws an exception (for references). This is the only C++ style cast that has a runtime performance cost. It can only be used with polymorphic types (i.e., classes with at least one virtual function).