Sometimes we want to explicitly convert an object coercion type to another type. For example, if you want to perform floating-point division in the following code:
int I, J;
Double slope = i/j;
It is necessary to use a method to explicitly convert I and/or J to double, which is called forced-type conversion.
Named coercion type conversions
A named coercion type conversion has the following form:
cast_name<type> (expression);
Where type is the target type of the conversion and expression is the value to be converted. If type is a reference type, the result is an lvalue, and Cast-name is one of the static_cast, dynamic_cast, Const_cast, and reinterpret_cast. Dynamic_cast supports run-time type recognition. CAST-NAME specifies what kind of conversion is performed.
Static_cast
Any explicitly defined type conversions can use Static_const as long as they do not contain the underlying const.
For example, you can make an expression perform floating-point division by casting an operand to a double type:
Double slope = static_cast<double> (j)/I;
Static_cast is useful when you need to assign a larger arithmetic type to a smaller type. At this point, coercion of type conversions tells the reader and compiler of the program: we know and don't care about the potential for precision loss. In general, if the compiler finds an arithmetic type that attempts to assign a value to a smaller type, a warning message is given, but when we perform an explicit type conversion, the warning message is closed.
Static_cast is also useful for type conversions that cannot be performed automatically by the compiler. For example, we can use static_cast to retrieve the value that exists in the void* pointer:
void *p = &d; Correct: The address of any extraordinary object can be deposited void*
Correct: Convert void* back to the original pointer type
Double *DP = static_cast<double*> (p);
When we hold the pointer in void* and use static_cast to cast it back to the original type, you should ensure that the value of the pointer remains the same. That is, the result of the cast will be equal to the original address value, so we must ensure that the resulting type is the type that the pointer refers to. Once the type does not match, it will produce undefined consequences.
Const_cast
Const_cast can only change the underlying const of an operand
const char *PC;
Char *p = const_cast<char*> (PC); Correct: But the Write value by P is undefined behavior
For the behavior of converting a constant object to a very mass object, we generally call it "removing the Const property (cast Away the const)". Once we have removed the const nature of an object, the compiler will no longer prevent us from writing to that object. If the object itself is not a constant, it is lawful to use a forced type conversion to obtain write permission. However, if the object is a constant, then performing a write operation with const_cast will result in undefined results.
Only const_cast can change the constant property of an expression, and using a different form of a named coercion type conversion to change the constant property of an expression will throw a compiler error. Similarly, it is not possible to change the type of an expression with const_cast:
Const Char *CP; // Error: static_cast cannot convert const property char* q = static_cast<char*> (CP); static_cast<string// Correct, string literals are converted to String type const_cast<string// error, const_cast only change constant property
In summary: Const_cast can only change the underlying const, that is, it can only alter the Const property of an object or reference, and cannot alter the object's Const property. Const_cast can add a const property, or it can remove a const property.
Reinterpret_cast
Reinterpret_cast usually provides a lower level of re-interpretation of the bitwise patterns of the operands. For example, suppose a conversion like this:
int *ip;
Char *pc = reinterpret_cast<char*> (IP);
We have to keep in mind that the real object that the PC refers to is int, not character, and if you use the PC as a normal character pointer, you might get a run-time error. For example:
String str (PO);
Run-time behavior that could cause an exception.
Using reinterpret_cast is very risky, and the example of PC initialization is a good proof of this. The key issue is that the type has changed, but the compiler does not give any warning or error messages. When we initialize a PC with an int address, the compiler does not issue any warnings or error messages because it explicitly declares that the conversion is legitimate. The next time the PC is used, it will assume that its value is the char* type, and the compiler has no way of knowing that it actually holds a pointer to int.
dynamic_cast
Used only for pointers and references to objects. When used with polymorphic types, it allows arbitrary implicit type conversions and the reverse process. However, unlike static_cast, in the latter case (note: The reverse process of implicit conversion), dynamic_cast checks whether the operation is valid. That is, it checks whether the transformation returns a valid full object that is requested. The detection occurs at run time. If the pointer being converted is not a valid full object pointer being requested, the return value is null. For reference types, a Bad_cast exception is thrown.
Several forced type conversions for C + +