An explicit type conversion, often referred to as a cast, is used (type) value in the C + + language (and can also take type (value)). The correctness of this conversion is entirely in the hands of programmers, and traditionally, coercion is often overused as a major source of error for C + + programs.
To reduce the side-effects of casts and to enable programmers to quickly locate (always the most questionable) casts in the wrong way, a new 4 keyword *_cast has been added to standard C + + to promote a new C + + explicit conversion syntax:
static_cast (Static Conversions)
Used to clearly define benign and moderately benign transformations, including automatic type conversions that originally did not require a cast ( including lossless boost conversions and narrowing conversions of potentially lost information [narrowing conversion], The latter compiler generally warns of this .) Standard C + + advocates the use of the new *_cast explicit type conversion method for any type of data conversion, whether automatic or mandatory. For example:
int i = 0X7FFF; Long L; float F; Char c;//(1) Typical non-forced conversion (auto-conversion)//Traditional way: L = i; F = New method advocated by i;//: L = static_cast<long> (i); f = static_cast<float> (i);//(2) narrowing conversion//Traditional way://will display warning message: i = l;//May lose the number i = f;//may be missing information C = i; can lose number//Do not display warning message (but still difficult to locate): i = (int) l; i = (int) F; c = (char) i;//advocated new way (does not display warning message, and easy to locate): i = static_cast<int> (l); i = static_cast<int> (f); c = static_cast<char> (i);
Const_cast (Constant Conversions)
You can convert a constant (const) of (the same data type) to a very type, and convert the volatile (volatile) type to a non-volatile variant. If used for other types of conversions, a compilation error is generally generated. For example:
const int i = 0; int *pi; PI = &i; Error PI = (int *) &i;//is against pi = const_cast<int *> (&i);//Perfect long *PL = Const_cast<long *&G t; (&i); Error volatile int k = 0;
dynamic_cast (Dynamic Conversion)
A secure down-type conversion (downcast) operation that moves down on a class inheritance level.
Because the base class for each derived class has only one, and the derived class itself contains almost all of the base class information (except for the private type), the upward type conversion (upcast) is always unique and more secure.
A base class tends to have more than one derived class, and it is common for derived classes to add some unique data and operations based on the base class, so the downward type conversion is always polymorphic and less secure.
The dynamic_cast provides a safe downward-type conversion operation, and the return value is the desired pointer only if the type conversion is correct and the conversion succeeds, otherwise it will return 0 (null null pointer), indicating that it is not the correct type.
For example:
Class Pet { //... }; Class Dog:public Pet { //... }; Class Cat:public Pet { //... }; Pet *ppet = new Cat; Type conversion up Dog *pdog = Dynamic_cast<dog *> (ppet);//Type error, return 0 (NULL) Cat *pcat = Dynamic_cast<cat *> (Ppet); Type is correct, return pointer
Note: Althoughdynamic_cast is secure, the runtime requires a certain amount of overhead, so it is not recommended to use this conversion extensively . If you have been able to confirm the correctness of the conversion, you can take the static_cast conversion (without runtime overhead) described previously. The dynamic_cast conversion is required only if you are not sure if the conversion is correct.
reinterpret_cast (re-interpreting conversions)
One of the most unsafe type conversions that is most likely to be problematic. This type of conversion is only necessary in the following situations: when it is necessary to use, the resulting thing is different, in order for it to be used for the original purpose, it must be converted back again.
For example:
const int SZ = 100; Defines the size of the array, which is advocated by standard C + + with a const variable (instead of a constant or symbol macro constants) struct x { int a[sz]; };//contains only an integer array of structures x x;//define structure variables, The value of the array element in the structure is meaningless (requires initialization) int *px = Reinterpret_cast<int *> (&x);//To initialize, first convert the structure to an int array for (int *i = px; I < px + sz; i++) *i = 0; Initializes the value of each array element to 0 print (reinterpret_cast<x *> (px)),//To the structure pointer for use, or to use the original identifier X directly, which is equivalent to print (&X) ;
Using reinterpret_cast is often an unwise and inconvenient way of programming. But it's also very useful when you have to use it.
Summary:
Of the four types of casts, static_cast is most commonly used (not yet popular, but is advocated by standard C + +), dynamic_cast most important, const_cast useful, and reinterpret_cast is seldom used.
Four types of C + + conversions