Solemnly declare: This article is written by the author based on my personal understanding. errors are inevitable. Please be prepared!
You can reprint, modify, and indicate the source when reprinting!
Static_cast
It is used for "benign" and "moderate benign" conversions, including non-forced conversions (such as automatic conversions ). Static_cast is all used for clearly defined conversions, including "safe" conversions that the compiler allows us to do without forced conversions and less secure but clearly defined conversions.
Usage: static_cast <Type-ID> (Expression)
This operator converts expression to the Type-ID type, but does not perform a type check at runtime to ensure the conversion security. Static_cast includes the following types:
A. Typical unforced Transformation
B. Narrow (with information loss) Transformation
C. Use void * for forced conversion
D. implicit type conversion
E. Static class-level positioning
A. It is safe to convert the pointer or reference of a subclass to the base class.
B. Downward conversion is insecure. It converts a base class pointer or reference to a subclass. Because it is a static conversion, dynamic_cast should be used because no type check is performed at runtime.
1 int i = 0x7fff; // max pos value = 32767 2 long l = 0; 3 float f = 1.0f; 4 5 // A: typeical castless conversions: 6 l = static_cast<long>(i); 7 8 // B: narrowing conversions: 9 i = static_cast<int>(f);10 11 // C: forcing a conversion from void* :12 void* pvoid = &i;13 float* pf = static_cast<float*>(pvoid);14 15 // D: Implicit type conversions, normally performed by the compiler16 double d = 0.0;17 int x = d; // Automatic type conversion18 x = static_cast<int>(d); // More explicit
Note: static_cast cannot convert the const, volitale, or _ unaligned attribute of expression.
Dynamic_cast
Usage: dynamic_cast <Type-ID> (Expression)
This operator converts expression to type-ID objects. Type-ID must be a class pointer, class reference, or void *;
If type-ID is a class pointer type, expression must also be a pointer. If type-ID is a reference, expression must also be a reference.
When dynamic_cast is used to convert a specific type down, the return value is a pointer to the desired type only when the type conversion is successful, otherwise, it returns 0 to indicate that the conversion fails.
Dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes.
Dynamic_cast andStatic_castThe results are the same;
When performing a downstream conversion, dynamic_cast has the type check function.Static_castMore secure.
1 #include <iostream> 2 using namespace std; 3 4 class CPet { public : virtual ~CPet() {} }; 5 class CDog : public CPet {}; 6 class CCat : public CPet {}; 7 8 int main() 9 {10 CPet* pPet = new CCat; // Upcast, safe11 // Try to cast it to CCat* :12 CCat* pCat = dynamic_cast<CCat*>(pPet); // cast success13 // Try to cast it to CDog* :14 CDog* pDog = dynamic_cast<CDog*>(pPet); // cast fail15 16 cout << "pDog = " << (long)pDog << endl;17 cout << "pCat = " << (long)pCat << endl;18 }
In the above Code segment, the ppet is successfully converted to the ccat * type (because CPET * ppet = new ccat;) and fails to be converted to the cdog * type, dynamic_cast checks the type.
When using dynamic_cast, you must perform operations on a truly multi-State hierarchy (that is, it contains virtual functions), because dynamic_cast uses information stored in the vtable to determine the actual type.
Note: whenever we perform a downward type conversion, we should perform a type check to ensure that the conversion is successful (that is, the return value is not 0 ). However, we do not need to make sure that the pointers are the same, because the pointers are usually adjusted (especially in the case of multi-inheritance) during up and down conversions ).
Dynamic _ cast requires a little extra overhead, but when executing a large number of dynamic_cast operations, it indicates that our design is faulty and will also lead to performance loss. To avoid additional overhead caused by calling dynamic_cast, you can use static_cast if you already know the type.For example, you can write the following code to convert ppet to the ccat * type:
CCat* pCat = static_cast<CCat*>(pPet);
Reinterpret_cast
This is the most insecure conversion mechanism and is most likely to cause problems. Reinterpret_cast assumes an object as a pattern (for some secret purpose), as if it is a completely different type object. This is a low-level bit mode, and C is not well-named. Before using reinterpret_cast to do anything, reinterpret_cast always needs to return to the original type (or think of the variable as its original type ). To put it bluntly, the original type is hidden and converted back when used; otherwise, the compilation fails.
Usage: reinterpret_cast <Type-ID> (Expression)
Type-ID must be a pointer, reference, arithmetic type, function pointer, or member pointer.
It can convert a pointer to an integer, or an integer to a pointer (first, convert a pointer to an integer,
You can also obtain the original pointer value after converting the integer to the original type ).
Const_cast
Const_cast is used to modify the const or volatile attributes of a const_cast type. It can convert const to non-const and volatile to non-volatile. This is the only conversion allowed by const_cast. Apart from the const or volatile modifier, The type_id and exdivssion types are the same.
Usage: const_cast <Type-ID> (Expression)
Constant pointers are converted to non-constant pointers and still point to the original object;
Constant reference is converted to a non-constant reference and still points to the original object. Constant object is converted to a non-constant object.
Voiatile and const classes. Take the following example:
1 class B 2 { 3 public: 4 int m_iNum; 5 } 6 void foo() 7 { 8 const B b1; 9 b1.m_iNum = 100; //comile error10 B b2 = const_cast<B>((b1);11 b2. m_iNum = 200; //fine12 }
The above code will report an error during compilation, because B1 is a constant object and cannot be changed;
Using const_cast to convert it into a constant object, you can change its data members at will. Note: B1 and B2 are two different objects.
Summary:
Dynamic_cast: usually used for conversion between the base class and the derived class;
Const_cast: converts const and volatile.
Static_cast: A Normal conversion. If you do not know which one to use, use this function.
Reinterpret_cast: used to convert a character pointer to an integer without any association.
References:
Http://blog.csdn.net/Lambol_8309/article/details/4534338
Http://blog.sina.com.cn/s/blog_4a84e45b0100f57m.html