What is a type conversion?
The meaning of a type conversion is to change the representation of a variable by changing its type to another type. To convert a simple object for a type to another object you would use the traditional type conversion operator.
C and C + + type conversions
In C:
Copy CodeThe code is as follows:
(t) element or T (Element)
In C + +:
Copy CodeThe code is as follows:
reinterpret_cast<t*> (expression)
dynamic_cast<t*> (expression)
static_cast<t*> (expression)
const_cast<t*> (expression)
four forms of forced transformation in C + + each for a specific purpose:
dynamic_cast is primarily used to perform "safe downcasting", that is, to determine whether an object is a specific type in an inheritance system. It is the only forced transformation that cannot be performed with the old style syntax, and it is the only forced transformation that can have significant runtime costs.
static_cast can be used to force implicit conversions (for example, a Non-const object to a const object, an int to a double, and so on), and it can also be used for a number of reverse transformations of such conversions (for example, the void* pointer is transformed into a typed pointer, The base class pointer is transformed to a derived class pointer), but it cannot transform a const object into a Non-const object (only const_cast can), which is closest to the C-style conversion.
const_cast is generally used to force the elimination of the constants of an object. It is the only one that can do this with the C + + style of coercion.
Reinterpret_cast is intended for the underlying forced transformation, resulting in the implementation of dependency (Implementation-dependent) (that is, non-portable) results, such as transforming a pointer into an integer. Such a forced transformation should be extremely rare outside of the underlying code.
A popular explanation:
| dynamic_cast |
Typically used when converting between a base class and a derived class |
| Static_cast |
General conversions, if you don't know which one to use, use this |
| Const_cast |
Conversions primarily for const and volatile |
| Reinterpret_cast |
Used for conversions that do not have any associations, such as converting a character pointer to an integer number |
Specific analysis:
1) static_cast<t*> (a) compiler processing at compile time
- Converting address A to type t,t and a must be pointers, references, arithmetic types, or enumeration types.
- Expression static_cast<t*> (a), the value of a is converted to the type T specified in the template.
- During the run-time conversion process, no type checking is performed to ensure the security of the conversion.
- Static_cast it can be converted between the built-in data types, and the class can only be converted between the linked pointer types. The pointer can be converted, converted, or converted to a type outside of the inheritance system.
Class A {...};
Class B {...};
Class D:public B {...};
void F (b* pb, d* PD)
{
d* PD2 = static_cast<d*> (PB); Not safe, PB may just be a pointer to B
b* PB2 = static_cast<b*> (PD); Safe for
A * PA2 = static_cast<a*> (PB); Error A and B have no inheritance relationship
...
}
2) dynamic_cast<t*> (a) At run time, check if the conversion is possible
- completes the promotion in the class hierarchy. T must be a pointer, reference, or untyped pointer. A must be an expression that determines a pointer or reference.
- Dynamic_cast can only be applied to pointers or references and does not support built-in data types
- The expression dynamic_cast<t*> (a) converts a value to an object pointer of type T. If the type T is not a base type of a, the operation returns a null pointer.
- It is not just like static_cast, which checks whether the two pointers before and after the conversion belong to the same inheritance tree, it also checks the actual type of the object referenced by the pointer and determines whether the conversion is feasible.
- If it can, it returns a new pointer and even calculates the necessary offsets to handle multiple inheritance. If the two pointers cannot be converted, the conversion fails, and a null pointer is returned (NULL).
- Obviously, in order for dynamic_cast to work properly, you must have the compiler support the runtime type information (RTTI).
Class Base {virtual dummy () {}};
Class Derived:public Base {};
base* B1 = new Derived;
base* b2 = new Base;
derived* D1 = dynamic_cast<derived *> (B1); Succeeds
derived* D2 = dynamic_cast<derived *> (b2); Fails:returns ' NULL '
3) const_cast<t*> (a) compiler processing at compile time
- To remove constants from a type, the T and a must be of the same type, except for const or unstable variable addresses.
- The expression const_cast<t*> (a) is used to remove these properties from a class: const, volatile, and __unaligned.
- For the type of const that is defined by itself, even if you remove the const, you should be careful when you manipulate this piece of content, only R cannot operate W, otherwise it will be wrong
- Const_cast operations cannot be converted between different types. Instead, it simply converts an expression that it acts into a constant. It can convert data that is not a const type to a const type, or remove the const attribute.
Class A {...};
void F ()
{
Const A *PA = new A;//const Object
A *pb;//Non-const object
PB = PA; There will be an error, and you cannot assign a const object pointer to a non-const object
PB = Const_cast<a*> (PA); It's OK now.
...
}
Const char* p = "123";
char* C = const_cast<char*> (p);
C[0] = 1; On the surface, the const is removed by compiling, but the system still does not allow the operation of its address.
4) reinterpret_cast<t*> (a) compiler processing at compile time
- Any pointer can be converted to another type of pointer, t must be a pointer, a reference, an arithmetic type, a pointer to a function, or a pointer to a class member.
- The expression reinterpret_cast<t*> (a) can be used for conversions such as char* to int*, or one_class* to unrelated_class*, and therefore may not be secure.
Class A {...};
Class B {...};
void F ()
{
A * pa = new A;
void* PV = reinterpret_cast<a*> (PA);
PV now points to an object of type B, which may be unsafe
...
}
A (summary) explanation of coercion type conversions based on C + +