The old c transformation method allows you to convert any type to any other type and has its own defects in the following two aspects:
The intention of the transformation cannot be more precise.
For example, transforming a pointer-to-base-class-object into a pointer-to-derived-class-object (changing the type of an object) and transforms a pointer-to-const-object into a pointer-to-non-const-object (changing the object's constants), which is not distinguished in the old c syntax.
Difficult to identify.
The syntax of the old c transformation mode is (type) expression, which is composed of a pair of parentheses and an object name. This syntax structure can be used anywhere in C ++, this makes it impossible to intuitively determine whether the operation is a transformation operation.
To solve the preceding disadvantages, C ++ introduces four new transformation operators:
Static_cast,Const_cast,Dynamic_cast,Reinterpret_cast
Syntax: *** _ cast <type> (expression ).
The following describes the usage of the four new operators:
Static_cast: Basically, it has the same power and significance as the old-style C transformation, as well as the same limitations. For example:
// Calculate the division of two int-type numbers and the result is double.
IntFirstnum, secondnum;
DoubleRes = (Double) Firstnum/secondnum;// Old c syntax
DoubleRes =Static_cast<Double> (Firstnum)/secondnum;// New C ++ transformation operator
Const_cast: Used to change the constness or volatileness in an expression ). For example:
IntNum;
Const Int* Cpnum = & num;
Int* Pnum = cpnum;// Error: cannot convert from 'const int * 'to 'int *'
Int* Pnum = (Int*) Cpnum;// Legacy C
Int* Pnum =Const_cast<Int*> (Cpnum );// New C ++ const_cast removes Constants
Dynamic_cast: Used to carry out the "safe downward or cross-system transformation actions" in the inheritance system ". For example:
// Dynamic_cast can be used to convert "pointer or reference pointing to base class Object" to "pointer or reference pointing to derived class Object"
// If the transformation fails, it will be expressed as a null pointer or an exception
ClassCbase {};// Base class
ClassCderived:PublicCbase {};// Inheritance class
Cderived DC;
Cderived * dp = & DC;
Cbase * BP =Dynamic_cast<Cbase *> (DP );// Use dynamic_cast to convert the pointer pointing to the inherited class to the pointer pointing to the base class
Cbase & Br =Dynamic_cast<Cbase &> (DC );// Use dynamic_cast to convert the reference pointing to the inherited class to the reference pointing to the base class
Reinterpret_cast: The most common purpose is to convert the "function pointer" type. For example:
typedef void (* funcptr) (); // funcptr is a void function pointer type without any parameter returned value
int ifunc () { return 0 ;}< span style =" color: #008000; "> // ifunc is a function with no parameter returned value of int type
void func (funcptr f) {}< span style = "color: #008000; "> // the func function parameter is a function pointer of the funcptr type.
main ()
{
func (ifunc (); // error: cannot convert parameter 1 from 'int' to 'void (_ cdecl *) (void) '
func (reinterpre_cast
(ifunc);
// right! Reinterpre_cast converts a function whose return value is int to a function whose return value is void
}