Post address: http://www.cnblogs.com/alexqdh/archive/2011/06/09/2075713.html
C ++ is not type-safe. Static_cast, dynamic_cast, const_cast, and reinterpret_cast
Why can I use the forced conversion of the C style to convert anything I want into a desirable type. Why do we still need a new forced conversion of the C ++ type?
The new type of forced conversion can provide better control over the forced conversion process, and allow control of various types of forced conversion. In C ++, the style is static_cast <type> (content ). The other advantage of forced conversion of C ++ styles is that they clearly indicate what they are going to do. A programmer can immediately understand the purpose of a forced conversion by looking at such code.
Differences between the four conversions:
Static_cast: converts the built-in basic data types in C ++.
int c=static_cast<int>(7.987);
?
If classes are involved, static_cast can only convert each other in Correlated types, not necessarily including virtual functions.
Class A {}; class B: public A {}; class C {}; int main () {A * a = new A; B * B; C * c; B = static_cast <B> (a); // No error is reported during compilation. Class B inherits Class A c = static_cast <B> (a); // an error is reported during compilation, class C has nothing to do with class A. return 1 ;}
Const_cast: The const_cast operation cannot be converted between different types. Instead, it only converts an expression to a constant. It can convert a data that is not of the const type to the const type, or remove the const attribute.
Reinterpret_cast: it has the same capability as C-style forced conversion. It can convert any built-in data type to any other data type, or convert any pointer type to another type. It can even convert the built-in data type to pointer, without the need to consider type security or constants. No.
Dynamic_cast:
(1) The other three types are completed during compilation. dynamic_cast is processed at runtime, and type check is required during runtime.
(2) cannot be used for forced conversion of built-in basic data types.
(3) If the conversion of dynamic_cast is successful, a pointer or reference to the class is returned. If the conversion fails, NULL is returned.
(4) using dynamic_cast for conversion,A virtual function must exist in the base class.Otherwise, the compilation fails.
B needs to check the reason for the existence of virtual functions: the existence of virtual functions in the class indicates that it has the situation that it wants to make the base class pointer or reference the object pointing to the derived class, then the conversion makes sense..
This is because the runtime type check requires runtime type information, which is stored in the virtual function table of the class (the concept of virtual function table, in <Inside c ++ object model>,
Only classes that define virtual functions have virtual function tables.
(5) during class conversion and uplink conversion between classes, dynamic_cast and static_cast have the same effect. Dynamic_cast provides the type check function for downstream conversions, which is safer than static_cast. The upward conversion refers to the downward conversion to the subclass object, that is, converting the parent class pointer to the subclass pointer. The success or failure of the downward conversion is also related to the type to be converted. That is, the actual type of the object to be converted must be the same as the type of the object to be converted. Otherwise, the conversion fails.
Example:
# Include <iostream >#include <cstring> using namespace std; class A {public: virtual void f () {cout <"hello" <endl ;};}; class B: public A {public: void f () {cout <"hello2" <endl ;};}; class C {void pp () {return ;}}; int fun () {return 1;} int main () {A * a1 = new B; // a1 is A type pointer pointing to a B type object A * a2 = new A; // a2 is A type pointer pointing to A type object B * B; C * c; B = dynamic_cast <B *> (a1); // The result is not null. The downward conversion is successful. a1 points to B type objects, so it can be converted to B type pointer.. If (B = NULL) {cout <"null" <endl;} else {cout <"not null" <endl ;} B = dynamic_cast <B *> (a2); // The result is null. if (B = NULL) {cout <"null" <endl ;} else {cout <"not null" <endl;} c = dynamic_cast <C *> (a); // The result is null, downward Conversion failed if (c = NULL) {cout <"null" <endl;} else {cout <"not null" <endl ;} delete (a); return 0 ;}
?