dynamic_cast: Typically used when converting between a base class and a derived class, Run-time cast
Const_cast: Mainly for const and volatile conversions.
Static_cast: General conversion, no run-time check. Usually, if you don't know which one to use, use this.
Reinterpret_cast: Used for conversions that do not have any associations, such as converting a character pointer to an integer number.
1) static_cast<t*> (a)
The compiler converts address a to type t,t and a must be pointers, references, arithmetic types, or enumeration types during compile-time processing
. The
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 built-in data types, and classes can only be converted between linked pointer types. The pointer can be converted and transformed in the inheritance system, but cannot be converted to a type other than the inheritance system
Class A {...};
class B {...};
Class D:public B {...};
void F (b* pb, d* pd)
{
d* pd2 = static_cast<d*> (pb); & nbsp; //is not secure, PB may be just a pointer to B
b* PB2 = static_cast<b*> (PD); //Secure
a* PA2 = static_cast<a*> (PB); //Error A and B have no inheritance relationship
}
2) dynamic_cast<t*> (a)
at run time, checks whether the conversion is possible. The
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. The
dynamic_cast can only be applied to pointers or references, does not support built-in data types
Expressions 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, but also examines 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).
3) const_cast<t*> (a)
compiler handles the constants in the type at compile time
, except for const or unstable variable addresses, T and a must be of the same type. The
Expression const_cast<t*> (a) is used to remove these properties from a class: const, volatile, and __unaligned.
class A {...};
void F ()
{
const A *pa = new A;//const object
a *pb;// Non-const object
&NBSP;&NBSP;&NBSP;&NBSP;//PB = PA;//Error here, cannot assign const object pointer to non-const object
&NBSP;&NBSP;&NBSP;&NBSP;PB = Const_cast<a*> (PA); Now OK.
....
}
for a type that is defined as const by itself, even if you remove the const, be careful when you manipulate the content, only R cannot operate W, or error
Const char* p = "123";
char* C = Const_cast<char*> (P);
C[0] = 1; //The const is removed by compiling on the surface, but the system still does not allow the operation of its address. The
Const_cast operation cannot be converted between different classes. 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.
Try not to use const_cast, if found to call their own function, unexpectedly used const_cast, then quickly stop, reconsider the design bar.
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, reference, 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
}
does not use reinterpret_cast in many cases, and is used only when it is necessary for other types of casts to not meet the requirements.
== ===========================================
= = Static_cast vs. reinterpret_cast
== ================================================
Reinterpret_cast is meant to map to a completely different type of meaning, and this keyword is used when we need to map the type back to the original type. The type we map to is just for the sake of the trick and other purposes, which is the most dangerous of all mappings. (This is the exact words in C + + programming Idea)
The static_cast and reinterpret_cast operators modify the operand type. They are not mutually reversible;
Static_cast uses type information at compile time to perform transformations that perform the necessary checks (such as pointer out-of-bounds calculations, type checking) on transformations. The operands are relatively safe.
Reinterpret_cast, on the other hand, is a mandatory type conversion in C + +, and the operator modifies the operand type, but simply interprets the given object's bit model without binary conversion.
Examples are as follows:
int n=9;
Double D=static_cast < double > (n);
In the example above, we convert a variable from int to a double. These types of binary expressions are different. To convert an integer 9 to a double integer, 9,static_cast needs to correctly complement the bit for the double-precision integer d. The result is 9.0.
And Reinterpret_cast's behavior is different:
int n=9;
Double d=reinterpret_cast<double & > (n);
This time, the results are different. After the calculation, D contains a useless value. This is because reinterpret_cast only replicates n bits to D and does not perform the necessary analysis.
Therefore, you need to be cautious about using reinterpret_cast.
The most common use of reinterpret_casts is to convert between function pointer types.
For example, suppose you have an array of function pointers:
Typedefvoid (*funcptr) ();//funcptr is a pointer to a function that has no arguments and a return value of type void
Funcptrfuncptrarray[10];//funcptrarray is an array that can hold 10 funcptrs pointers
Let's assume you want (for some inexplicable reason) to put a pointer to the following function into the Funcptrarray array:
int dosomething ();
You can't just do this by typing, because the dosomething function has an incorrect type for the Funcptrarray array. The function return value in the Funcptrarray array is type void, and the DoSomething function return value is of type int.
FUNCPTRARRAY[0] = &dosomething;//Error! Types do not match
Reinterpret_cast allows you to force the compiler to look at them in your own way:
Funcptrarray[0] = reinterpret_cast<funcptr> (&dosomething);
The code for the conversion function pointer is not portable (c + + does not guarantee that all function pointers are represented in the same way), and in some cases such conversions produce incorrect results
Go C + + coercion type conversion