We have four specific castingoperators:dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses.
dynamic_cast <new_type> (expression)
Reinterpret_cast <new_type> (expression)
Static_cast <new_type> (expression)
Const_cast <new_type> (expression)
One, to C + + four types of conversion summarized as follows:
Const_cast (expr)
Constant for removing objects (Cast away the constness)
Const_cast is generally used for pointers or references
The purpose of using const_cast to remove a const qualifier is not to modify its contents
Use Const_cast to remove a const qualifier, usually for a function to accept this actual argument
static_cast (expr)
Any type conversions implicitly executed by the compiler can be completed by static_cast
When a larger arithmetic type is assigned to a smaller type, it can be cast with static_cast.
You can convert a void* pointer to a pointer of a type
You can convert a base class pointer to a derived class pointer
Cannot convert const to Nonconst, this only const_cast can be done
Reinterpret_cast (expr)
"Typically provides a lower layer of interpretation for the bit pattern of an operand" means to interpret the data in the form of binary existence.
int i;
Char *p = "This is a example.";
i = reinterpret_cast<int> (p);
At this point, the value of I and P is exactly the same.
int *ip
Char *pc = reinterpret_cast<char*> (IP);
Programmers need to remember that the real object the PC points to is int, not string.
If you manipulate your PC as a character pointer, you may cause a run-time error
such as int len = strlen (PC);
dynamic_cast (expr)
Perform a "security down" transition operation, which means supporting the Run-time identification pointer or the object being pointed to, which is the only transition operation that cannot be done in the old-fashioned language.
Dynamic_cast is the most stringent conversion, static_cast second, and reinterpret_cast is the most relaxed. If you encounter a problem where you can't turn an integer into a function pointer, you can solve it like this:
Reinterpret_cast<lpfun&> (naddress);
Note that lpfun here has a "&" symbol, refers to a reference, C + + reference is actually implemented with pointers, and these "transformations" are actually the conversion of pointers, so plus the reference symbol compiled to pass.