Think about the typing type conversion function (CAST) in the program design, it is as despise like a GOTO statement. But it is still not unbearable, because when certain critical junctions, type conversion is still required, then it is a necessity.
However, the type conversion of the C style does not represent all type conversion functions.
They are too rude, allowing you to convert between any types. But if you want to make more accurate type conversion, this will be an advantage. There is a huge difference in these types of conversion, such as converting a pointer-to-const-object pointing to the Const object into pointer to a non-Const object (ie one Only the type conversion of Const is removed, and a pointer to the base class is converted into a pointer to the subclass (ie, completely changing the object type). Traditional C style type conversion does not distinguish between the two conversions. (This is not surprising, because the type of C style is designed for C language, not for C language).
The type of C-style is difficult to identify in the program statement. In syntax, type conversion consists of parentheses and identifiers, and these can be used anywhere in C . This makes it difficult to answer the problem like this about type conversion: "Is the type conversion in this program?". This is because manual reading is likely to ignore the type of conversion statement, and use the toolplicer like GREP to distinguish them from statements.
C overcome the disadvantages of C style type conversion by introducing four new type conversion operators, these four operators are, static_cast, const_cast, dynamic_cast, and reinterpret_cast. In most cases, you only need to know that you are used to writing, for these operators.
(TYPE) Expression
And now you should always write this:
Static_cast
For example, suppose you want to convert an int to Double so that the expression containing the INT type variable produces a floating point value. If you use C style, you can write this:
INT firstnumber, secondnumber; ... double result = ((double) firstnumber / secondnumber;
If you use the above new type conversion method, you should write this:
Double Result = static_cast
Such type conversions are easily identified regardless of manual or for procedures.
STATIC_CAST is basically as strong as powerful and meaningful in functionality, meaning. It also has functions. For example, you can't use Static_CAST icon to convert Struct into an int type or convert the double type to a pointer type like a type of C style, and Static_CAST cannot remove the const attribute from the expression because another new type of conversion operator Const_cast has such a function. Other new C type conversion operators are used in a place where more restrictions are needed. Const_cast is used to convert the const or volatileness attribute that transitions the expression. By using const_cast, you emphasize you and the compiler that you want to do through the type conversion just change some of the constness or volatileness attributes. This meaning is constrained by the compiler. If you try to use const_cast to complete something other than the Constness or VolatileAss property, your type conversion will be rejected. Here are some examples: class widget {...}; class specialWidget: public widget {...}; void update (SpecialWidget * PSW); SpecialWidget SW; // SW is a non-Const object. Const SpecialWidget & CSW = SW; // CSW is a reference to SW // It is a const object Update (& CSW); // error! Cannot pass a const specialWidget * variable // give a function update for processing the SpecialWidget * type variable ( Const_cast
So far, const_cast's most common use is to convert the const attribute of the object.
The second special type converter is Dynamic_cast, which is used to convert downwardly along the inheritance relationship of the class. That is to say, you can use Dynamic_CAST to convert pointers or references to the base class to point to its derived class or its brothers, and you can know if the conversion is successful. Failed conversions will return an empty pointer (when the pointer is converted) or throw an exception (when the reference is converted):
Widget * pw; ... update (Dynamic_cast
The last one in these four types of converters is ReinterPret_cast. The type of conversion using this operator is almost all implementation-defined. Therefore, it is difficult to transplant using ReinterPret_Casts.
The most common use of ReinterPret_casts is to convert between the function pointer type. For example, suppose you have a function pointer array:
TypeDef void (* funcptr) (); // Funcptr is a pointer pointing to function //, the function is not parameter // return value type to voidfuncptr funcptray [10]; // Funcptrarray is a pointer to hold // 10 FuncPTRS pointers Array
Let us assume that you want (because some inexplicable reasons) put a pointer to the following functions into the FuncPtrarray array:
INT DOSMETHING ();
You can't do it directly without type conversion, because the DOSMETHING function has an error type for the Funcptraray array. The function return value in the FuncptraRray array is a Void type, and the DOSMETHING function return value is an int type.
Funcptrarray [0] = & dosomething; // Error! Types do not match ReinterPret_cast_cast allows you to force the compiler to look at them with your method: Funcptrarray [0] = // this compilesreinterpret_cast
The code of the conversion function pointer is unmiglable (C does not guarantee that all function pointers are represented by the same method), in some cases such a conversion generate incorrect results (see Terms M31), so you should avoid conversion Function pointer type unless you are in a critical moment with a warhead and sharp knife throat. A sharp knife. A very sharp knife.
If you use the compiler lack support for new type conversion methods, you can use traditional type conversion methods instead of static_cast, const_cast, and reinterpret_cast. You can also replace the next macro to simulate new type conversion syntax: #define static_cast (type, expr) ((use)) # define const_cast (type, expr) ((tPR) # define reinterpret_cast (TYPE, EXPR) ((Type) (EXPR))
You can use it like this:
Double Result = static_cast (double, firstnumber) / secondnumber; update (const_cast (specialwidget *, & sw)); funcptrarray [0] = reinterpret_cast (funcptr, & dosomething);
These analog will not be as safe as a real operator, but when your compiler can support new types of transitions, they simplify the process of upgrading the code.
There is no easy way to simulate the operation of Dynamic_cast, but many of the libraries provide functions, securely transition between derived classes and base classes. If you don't have these functions and you have to make such a type of conversion, you can also return to the type conversion method of the C style, but you will not know if the type conversion failed. Of course, you can also define a macro to simulate the functionality of Dynamic_cast, just like simulating other types of conversion:
#define Dynamic_cast (Type, EXPR) (Type) (EXPR)
Keep in mind that this simulation does not fully realize the functionality of Dynamic_cast, it doesn't matter to know if the conversion failed.
I know, yes, I know that new type conversion operators are not very beautiful and it is very troublesome to type with keyboards. If you find that they seem to be annoying, the type of C-style can continue to use and legal. However, it is precisely because the new type converter lacks aesthetic to make it uphold in the disadvantage of impact and recognition. Also, programs with new types of converters are more likely to be parsed (whether it is for manual or for tool programs, they allow compilers to detect errors that cannot be discovered. These are strong reasons for giving up the C style type conversion method. There is also a third reason: perhaps the type of converter is not beautiful and typed in trouble.