Forced conversions Four types it's possible that a lot of people tend to ignore it just like me, but sometimes it's more useful. Do not understand the proposal to see, some mechanisms I do not know very well, just to write some usage to let everyone see.
2004-11-27 9:00
Forced conversions are one of the ugliest features in C + +, both in terms of syntax and semantics. But the semantic ambiguity of the transformation based on C-style and some potential problems. Forced type conversions are ultimately accepted by C + +.
1.static_cast arithmetic Symbols
Static_cast<t> (e), Stroustrup allows us to see it as the inverse of the display of implied transformations. There is some truth to this, based on the implicit conversion of the object type we can use the static_cast conversion operation symbol. It is static detection and cannot be run-time detection type, especially in inheritance.
Scope of Use
<1> for conversions between all system types, not for system type pointer type conversions
Double t_d = 0;
int t_i= static_cast<int> (t_d); It's a legal conversion.
And trying to double*->int* is not allowed.
<2> for conversions between inheriting classes (with pointers), not for conversions between other object types that do not have an implicit conversion
Examples of inheritance:
Class X
{
};
Class Y:public X
{
};
Use: x t_o_x;
Y t_o_y = static_cast<y> (t_o_x); x* y* conversions can also be done because X, y inheritance off
Type can be automatically converted using the
An example of implicit conversions:
Class X
{
};
Class Y
{public:
Y (x i_x) {}
};
x t_o_x;
Y t_o_y = static_cast<y> (t_o_x); You see that Y-constructors can implicitly convert X-type
So you can put x->y, if you attempt to y->x will be an error
2.reinterpret_cast operations
The type information can be incomplete, primarily for conversions of type pointer types, such as some_type*, special_type*. It allows any pointer to be converted to another type of pointer, or any integer type to any pointer-type conversion (BT). The result is extremely insecure and cannot be safely applied to other purposes unless it is converted to the original type.
<1> use all shaping to convert to any type of pointer (the pointer is a 4-byte long, so the machine thinks the same type can be converted)
int C;
x* p = reinterpret_cast<x*> (c); X is any type of customization, of course including system type
<2> can convert between any type of pointer
y* C;
x* p = reinterpret_cast<x*> (c);//x,y represents all custom or system types
You can see that the transformation of reinterpret_cast is extremely irresponsible, he just conversion does not detect whether it can be converted.
<3> const_cast Operation symbols
This is simple from the name you can see, just to remove or add the const modifier symbol. However, for a type that is defined as const, even if you remove the const, be careful when you manipulate the content, only R cannot operate W, or it will be wrong.
Const char* p = "123";
char* C = const_cast<char*> (p);
C[0] = 1; The const is removed on the surface by compilation, but the system still does not allow this when manipulating its address
To do. This is a loophole.
<4> dynamic_cast Operation symbols
Scott Mayers describes it as an implementation of the inheritance system: a security-down transition or a cross-system transition action. In other words, you can use dynamic_cast to transform pointers or references to base class into pointers or references to objects that point to subclasses.
Class B {}; Polymorphic type contains virtual to dynamic_cast
Class D:public B {}
void F (b* pb)
{
d* PD1 = dynamic_cast<d*> (PB);//If PB is returned correctly for type D, if it is not returned 0
d* PD2 = static_cast<d*> (PB); No matter how it is, returning pointers may point to inappropriate
Static detection only and cannot be shipped
Whether the information for the row object is truly of type D
}
Anyway everyone in use know how to use OK, C + + forced conversion in the template is still very useful, other times I also like to use C conversion convenient. ^_^
Cast-in-C + + conversions