Http://hb.qq.com/a/20110722/001452.htm
The forced type conversion (Type Cast) of the C style is very simple. No matter what type conversion is:
Type B = (type)
The C ++ style type conversion provides four types of conversion operators for different applications.
Const_cast, literally, is to remove the const attribute.
Static_cast, which is named static type conversion. For example, convert int to Char.
Dynamic_cast, which is named as dynamic type conversion. For example, the polymorphism type conversion between subclass and parent class.
Reinterpreter_cast, only re-interpreting the type, but no binary conversion is performed.
Four types of conversion formats, such:
Type B = static_cast (type) ()
Const_cast
Remove the const or volatile attribute of the type.
Struct SA {
Int I;
};
Const Sa Ra;
// Ra. I = 10; // directly modify the const type and the compilation is incorrect.
Sa & RB = const_cast <SA &> (RA );
RB. I = 10;
Static_cast
It is similar to the forced conversion of the C style. Unconditional conversion, static type conversion. Used:
1. Conversion between the base class and the subclass: it is safe to convert the subclass pointer to the parent class pointer, but it is not safe to convert the parent class pointer to the subclass pointer. (Dynamic_cast is recommended for dynamic type conversion between base class and subclass)
2. convert basic data types. Enum, struct, Int, Char, float, etc. Static_cast cannot be used to convert irrelevant types (such as non-base classes and sub-classes.
3. convert a null pointer to a null pointer of the target type.
4. convert any type of expression to void type.
5. static_cast cannot remove the const and volitale attributes of the type (use const_cast ).
Int n = 6;
Double D = static_cast <double> (n); // converts the basic type.
Int * Pn = & N;
Double * D = static_cast <double *> (& N) // irrelevant type pointer conversion, compilation Error
Void * P = static_cast <void *> (PN); // convert any type to void type
Dynamic_cast
Conditional conversion, dynamic type conversion, and runtime type security check (return NULL if conversion fails ):
1. Secure conversion between the base class and the subclass.
2. You must have a virtual function.
3. Cross conversion between different subclasses of the same base class. But the result is null.
Class baseclass {
Public:
Int m_inum;
Virtual void Foo (){};
// The base class must have virtual functions. Dynamic_cast can be used only when multiple features are maintained
};
Class derivedclass: Public baseclass {
Public:
Char * m_szname [100];
Void bar (){};
};
Baseclass * pb = new derivedclass ();
Derivedclass * pd1 = static_cast <derivedclass *> (PB );
// Subclass-> parent class, static type conversion, correct but not recommended
Derivedclass * Pd2 = dynamic_cast <derivedclass *> (PB );
// Subclass-> parent class, dynamic type conversion, correct
Baseclass * PBS = new baseclass ();
Derivedclass * pd21 = static_cast <derivedclass *> (master );
// Parent class-> subclass, static type conversion, dangerous! Access subclass m_szname member out of bounds
Derivedclass * pd22 = dynamic_cast <derivedclass *> (master );
// Parent class-> subclass, dynamic type conversion, secure. The result is null.
Reinterpreter_cast
Only re-interpret the type, but no binary conversion is performed:
1. The conversion type must be a pointer, reference, arithmetic type, function pointer, or member pointer.
2. bitwise conversion. It can convert a pointer to an integer, or an integer to a pointer (first, convert a pointer to an integer, and then convert the integer to the original type of pointer, you can also get the original pointer value ). However, you cannot convert a non-32bit instance to a pointer.
3. The most common purpose is to convert between function pointer types.
4. It is difficult to ensure portability.
Int dosomething () {return 0 ;};
Typedef void (* funcptr )();
// Funcptr is a pointer to a function. This function has no parameters and the return value type is void.
Funcptr funcptrarray [10];
// The array of 10 funcptrs pointers allows us to assume that you want (for some inexplicable reason) to store a pointer to the following function into the funcptrarray array:
Funcptrarray [0] = & dosomething;
// Compilation error! Type Mismatch. reinterpret_cast allows the compiler to view them in your way: funcptrarray
Funcptrarray [0] = reinterpret_cast <funcptr> (& dosomething );
// Convert different function pointer types
Summary
Use const_cast to remove the const attribute.
Use static_cast to convert basic types.
Daynamic_cast is used for type conversion between Polymorphism classes.
Reinterpreter_cast is used to convert pointer types of different types.