Reprint: http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html
C-style forced type conversion (type cast) is simple, no matter what type of conversion is:
Type B = (type) A.
C + + Style type conversion provides 4 types of conversion operators to handle applications in different situations.
Const_cast, the literal understanding is to go to the const attribute.
Static_cast, the name is understood to be a static type conversion. such as int converted to char.
dynamic_cast, the name is understood to be a dynamic type conversion. such as a polymorphic type conversion between a subclass and a parent class.
Reinterpret_cast, only the type is re-interpreted, but there is no binary conversion.
4 types of conversion formats, such as: type B = static_cast (type) (a).
Const_cast
Removes the const or volatile property of a type.
1 struct SA {
2 int i;
3};
4 const SA RA;
5//RA.I = 10; Modify const type directly, compile error
6 SA &RB = const_cast<sa&> (RA);
7 RB.I = 10;
Static_cast
Similar to C-style casts. Unconditional conversion, static type conversion. For:
1. Conversion between a base class and a subclass where a child class pointer is safe to convert to a parent pointer, but it is unsafe for a parent pointer to be converted to a child class pointer. (Dynamic type conversion between base class and subclass recommended with dynamic_cast)
2. Basic data type conversions. enum, struct, int, char, float, and so on. Static_cast cannot perform conversions between unrelated types (such as non-base classes and subclasses).
3. Convert the 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, Volitale attribute of the type (with Const_cast).
1 int n = 6;
2 Double D = static_cast<double> (n); Basic Type Conversions
3 INT*PN =&n;
4 Double*d = static_cast<double*> (&n)//unrelated type pointer conversion, compilation error
5 void*p = static_cast<void*> (PN); Convert any type to void type
dynamic_cast
Conditional conversions, dynamic type conversions, run-time type security checks (conversion failure returns NULL):
1. Secure the 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.
1 class BaseClass {
2 Public:
3 int m_inum;
4 virtualvoid foo () {}; The base class must have a virtual function. Maintain multiple features to use dynamic_cast
5};
7 class Derivedclass:public BaseClass {
8 Public:
9 char*m_szname[100];
Ten void Bar () {};
11};
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* PB2 =new baseclass ();
DerivedClass *pd21 = Static_cast<derivedclass *> (PB2); Parent class, subclass, static type conversion, Danger! Access Subclass M_szname member out of Bounds
DerivedClass *pd22 = Dynamic_cast<derivedclass *> (PB2); Parent class, subclass, dynamic type conversion, secure. The result is null
Reinterpret_cast
Only the type is re-interpreted, but there is no binary conversion:
1. The type of conversion must be a pointer, reference, arithmetic type, function pointer, or member pointer.
2. Convert at the bit level. It can convert a pointer to an integer, or an integer to a pointer (a pointer is converted to an integer, the integer is converted to the original type of pointer, and the original pointer value can be obtained). However, you cannot convert an instance that is not 32bit to a pointer.
3. The most common use is to convert between function pointer types.
4. Portability is difficult to ensure.
1 int dosomething () {return0;};
2 typedef void (*FUNCPTR) (); Funcptr is a pointer to a function that has no arguments and a return value of type void
3 Funcptr funcptrarray[10]; An array of 10 funcptrs pointers Let's assume you want (for some inexplicable reason) to put a pointer to the following function into the Funcptrarray array:
5 Funcptrarray[0] =&dosomething;//compile Error! Type mismatch, reinterpret_cast can let the compiler look at them your way: Funcptrarray
6 funcptrarray[0] = reinterpret_cast<funcptr> (&dosomething); Convert between different function pointer types
Summarize
Go to the const attribute with const_cast.
Basic type conversion with static_cast.
A type conversion between polymorphic classes is used with Daynamic_cast.
Different types of pointer-type conversions with reinterpret_cast.
C + + type cast < go >