C ++ forced transformation

xiaoxiao2021-03-06  36

Forcing four types of mandatory transformation may often be ignored as me, but sometimes it is more useful. I don't know the suggestion. I don't know much about some mechanisms, just write some usage to let everyone look. 2004-11-27 9:00

Forced transformation, whether from grammar or in words, it is one of the most difficult features in C . However, the unclearity of the semantics of C-style transformation and some potential problems are there. Mandatory type transformation is ultimately accepted by C . 1.static_castal operation symbol Static_cast (e), Stroustrup allows us to see it as an implicit conversion. This is a sense of truth. We can use the static_cast to transform the operation symbol based on implicit conversion. It is static detection, which cannot be runtably detected, especially in inheritance. Use the range <1> for transformation between all system types, can not be used for system type pointer type transformation double t_d = 0; int T_i = static_cast (t_d); // is a legal transformation to try to put Double * > int * is the transformation between <2> for inherited classes (including pointers), cannot be used for transformation inheritance between other object types without implicit conversion: Class X {}; Class Y: public X {}; use: x t_o_x; y t_o_y = static_cast (t_o_x); // x * y * Transformation can also be performed because of X, Y, the type //, the type can automatically implicitly converted using implicit conversion Example: Class X {}; Class Y {

Public: y (x i_x) {}}; x t_ox; y t_o_y = static_cast (t_o_x); // You can see Y constructor can be implicitly converted to X-type, so you can use x> y, If you attempt to report Y-> X 2. Reinterpret_cast operation is mainly used for forced transformation of type pointer type, Some_Type * -> Special_Type * This can be transformed, and the type information can be incomplete. It allows arbitrary pointers to other types of pointers, also allowing any integer type to any pointer type transformation (BT). The resulting result is extremely unsafe, unless otherwise applied, unless it is transformed into the original type. <1> Using all places can be converted to any type of pointer (the pointer is a 4-byte Long's stuff, then the machine thinks that the same type can be converted) INT C; x * p = reinterpret_cast (c) // x is a custom arbitrary type, of course, including system type <2> can transform Y * C between arbitrary type pointers; x * p = reinterpret_cast (c); // x, y represents all Custom or system types You can see that ReinterPret_cast's transformation is extremely irresponsible, and he only conveys whether it can be converted. <3> Const_cast Operation Symbol This is very simple from the name. You can see it, just to remove or add a const modifier symbol. But for the type of Const, even if you remove ConSt, you should be careful when you operate this content, you can not operate, otherwise it will be wrong. Const char * p = "123"; char * c = const_cast (p); C [0] = 1; // On the surface, the system still does not allow this when it is compiled by compiling. / / Do it. This is a vulnerability. <4> Dynamic_cast Operation Symbol Scott Mayers describes it as used to perform inheritance systems: safe down transformation or cross-transformation action. That is to say you can, use Dynamic_cast to point to the Base Class's pointer or a reference to the object of the object to the subclass. Class B {}; // Polymorphic Types Virtual Dynamic_CastClass D: Public B {} VOID F (B * PB) {D * PD1 = DYNAMIC_CAST (PB); // If the PB is returned correctly, If not returning 0 d * pd2 = static_cast (pb); // None, return pointer, it is possible to point to an inappropriate pair //, because STATIC is only static, and cannot be shipped //. Whether the information is truly D type} Anyway, everyone knows how to use OK, C forced transformation is still very useful in the template, and other times I also like to use C. Conversion. ^ _ ^

转载请注明原文地址:https://www.9cbs.com/read-66967.html

New Post(0)