C ++ introduces four new type conversion operators to overcome the disadvantages of C style type conversion. These four operators are:
Const_cast (Expression)
Dynamic_cast (Expression)
Static_cast (Expression)
Reinterpret_cast (Expression)
In most cases, you only need to know that you are used to writing these operators as follows:
(Type) expression
Now you should always write like this:
Static_cast (expression)
For example, if you want to convert an int to a double type, the expression containing the int type variable will generate a floating point value. If you use C-style type conversion, you can write as follows:
Int firstNumber, secondNumber;
...
Double result = (double) firstNumber)/secondNumber;
If you use the new type conversion method, you should write as follows:
Double result =Static_cast (FirstNumber)/secondNumber;
This form is easily identified, either for humans or for tool programs.
These four transformation operators perform their respective duties:
LConst_castIt is usually used to convert the object's constants to the constness (cast away the constness ). It is the only C ++-style transformation operator with this capability.
LDynamic_castIt is used to perform security downward or cross-system transformation actions in the inheritance system. That is to say, you can use it to convert a pointer or reference to a base class object to a pointer or reference to a derived class object, and determine whether the transformation is successful. If the transformation fails, it will be expressed as a null pointer (when the transformation object is a pointer) or an exception (when the transformation object is a reference. Dynamic_cast is the only transformation action that cannot be executed by the old syntax. It is also the only transformation action that may consume significant operation costs.
LStatic_castBasically, it has the same power and significance as the old-style C transformation and the same restrictions. For example, convert a non-const object to a const object, or convert an int to a double object. It can also be used to perform reverse conversions of the preceding conversions, such as converting the void * pointer to the typed pointer and converting the pointer-to-base to the pointer-to-derived. However, he cannot convert the const to non-const. Only the const-cast can do this.
LReinterpret_castThe intention is to execute a low-level transformation. The actual action and result may depend on the compiler, which means it cannot be transplanted. For example, convert a pointer to int. This type of transformation is rare outside of low-level code.
The old-style transformation is still valid in C ++, but it is recommended to use a new form here. First, they are easier to identify in code (not only for humans, but also for tools like grep), thus simplifying the process of "identifying where type systems are destroyed. Second, the narrower the goal of each transformation action, the more likely the compiler will diagnose the wrong application. For example, if you want to remove constants, you cannot compile them unless you use const_cast in the new transformation.
References:
Objective c ++ 3th p117
More effective c ++ p12 ~ 16