C + + forced type conversion Q: What is a C-style conversion? What are static_cast, dynamic_cast and reinterpret_cast? What's the difference? Why should you pay attention?
A: The meaning of the 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. For example, to convert a pointer of a floating-point number of type doubole to an integral type:
Code:
int i;
Double D;
i = (int) d; or: i = int (d); Works well for simple types that have standard definition transformations. However, such a translator can also be applied to classes (class) and class pointers without indiscriminate. The ansi-c++ standard defines four new conversions: ' Reinterpret_cast ', ' static_cast ', ' dynamic_cast ' and ' const_cast ', to control the type conversion between classes (Class).
Code:
1 reinterpret_cast<new_type>(expression)2 dynamic_cast<new_type>( Expression)3 static_cast<new_type>(expression)4 Const_cast<new_type > (expression)
1 reinterpret_cast
Reinterpret_cast converts a pointer to a pointer of another type. It also allows conversion from one pointer to an integer type. Vice versa. Is the pointer specific address value as an integer value? )
This operator can be converted between non-related types. The result of the operation is simply a binary copy of the value from one pointer to another pointer. Content that is pointed to between types does not have any type of check and conversion. If the case is a copy from a pointer to an integral type, the interpretation of the content is system-dependent, so any implementation is not convenient. A pointer that is converted to an integer large enough to contain it can be converted back to a valid pointer.
Code:
1 class A {}; 2 class B {}; 3 New A; 4 b * b = reinterpret_cast<b *>(a); 5 reinterpret_cast treats the type conversions of all pointers as if they were traditional type conversions.
2 static_cast
Static_cast allows arbitrary implicit conversions and inverse conversion actions to be performed. (even if it is not allowed implicitly)
This means that it allows a pointer of the subclass type to be converted to a pointer of the parent class type (this is a valid implicit conversion), and can also perform the opposite action: transform the parent class to its subclass .
In this final example, the converted parent class is not checked for consistency with the destination type.
Code:
1 classBase {};2 classDerived: PublicBase {};3 4Base *a =NewBase;5Derived *b = static_cast<derived *>(a);6 static_cast, in addition to manipulating type pointers, can be used to perform explicit conversions of type definitions, as well as standard conversions between the underlying types:7 Code:8 DoubleD =3.14159265;9 inti = static_cast<int> (d);
3 dynamic_cast
dynamic_cast is 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.
Code:
1 classBase {Virtualdummy () {}};2 classDerived: PublicBase {};3 4base* B1 =NewDerived;5base* B2 =NewBase;6 7derived* D1 = dynamic_cast<derived *> (B1);//succeeds8derived* D2 = dynamic_cast<derived *> (b2);//fails:returns ' NULL '
If a reference type performs a type conversion and this conversion is not possible, an Bad_cast exception type is thrown:
Code:
1 classBase {Virtualdummy () {}};2 classDerived: PublicBase {};3 4base* B1 =NewDerived;5base* B2 =NewBase;6 7Derived D1 = dynamic_cast<derived &*> (B1);//succeeds8Derived D2 = dynamic_cast<derived &*> (b2);//Fails:exception Thrown
4 const_cast
This conversion type manipulates the Const property of the passed object, or is either set or removed:
Code:
1 class C {}; 2 Const New C; 3 C *b = Const_cast<c *> (a);
The other three types of operators cannot modify the constant nature of an object.
Note: ' Const_cast ' can also change a type of volatile qualifier.
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.
casts (static_cast):
C++primer---p145
1 int Ten ; 2 double dval = (double) ival; 3 Double Double (ival); // Both of these effects are the same. 4double dval2 = static_cast<double> (ival); // This is also forced conversion, there are other const_cast, dynamic_cast, reinterpret_cast and other forced conversion, the function is not the same.
Static_cast:
Any explicitly defined type conversions, as long as they do not contain the underlying const, can use static_cast.
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 a large 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.
void* p = &d; Correct: The address of any extraordinary object can be deposited void*
Double *DP = static_cast<double*> (p); Correct: The void* conversion will be the initial pointer type
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.
C + + coercion type conversion