Q:What is C style conversion? What are static_cast, dynamic_cast and reinterpret_cast? What's the difference? Why should we pay attention?
#A: The meaning of conversion is to change the representation of a variable by changing the type of the variable to another type. To cast one simple object to another you would use traditional type conversion operators.
For example, to convert a pointer of a floating point number of type double to an integer:
Code
int i; double d; i = (int) d;
Or:
i = int (d);
Works well for simple types with standard defined conversions. However, such conversion operators can also be applied indiscriminately to classes and class pointers. The ANSI-C++ standard defines four new conversion operators: 'reinterpret_cast', 'static_cast', 'dynamic_cast' and 'const_cast', which are intended to control type conversion between classes.
Code:
reinterpret_cast<new_type>(expression) dynamic_cast<new_type>(expression) static_cast<new_type>(expression) const_cast<new_type>(expression)
1 reinterpret_cast
reinterpret_cast converts a pointer to a pointer of another type. It also allows conversion from a pointer to an integer type. vice versa. (Annotation: Is the specific address value of the pointer as an integer value?)
This operator can convert between unrelated types. The result of the operation is simply a binary copy of the value from one pointer to another pointer. Content pointed to between types does not undergo any type checking or conversion. If the case is a copy from a pointer to an integer, the interpretation of the content is system dependent, so no implementation is convenient. A pointer converted to an integer large enough to contain it can be converted back to a valid pointer.
Code:
class A {}; class B {}; A * a = new A; B * b = reinterpret_cast<B *>(a);
reinterpret_cast treats all pointer type conversions just like traditional type conversions.
2 static_cast
static_cast allows arbitrary implicit conversions and reverse conversions. (Even if it is not allowed to be implicit)
means that it allows a pointer of a subclass type to be converted to a pointer of a superclass type (this is a valid implicit Conversion), and at the same time, can also perform the opposite action: convert a parent class to its subclass. In this last example, the converted parent class is not checked for consistency with the destination type.
Code:
class Base {}; class Derived : public Base {}; Base *a = new Base; Derived *b = static_cast<Derived *>(a);
static_cast In addition to operating type pointers, it can also be used to perform explicit type definitions Conversions, as well as standard conversions between underlying types:
double d = 3.14159265; int i = static_cast<int>(d);
class Base { virtual dummy() {} }; class Derived : public Base {}; Base* b1 = new Derived; Base* b2 = new Base; Derived* d1 = dynamic_cast<Derived *>(b1); // succeeds Derived* d2 = dynamic_cast<Derived *>(b2); // fails: returns 'NULL'
class Base { virtual dummy() {} }; class Derived : public Base { }; Base* b1 = new Derived; Base* b2 = new Base; Derived d1 = dynamic_cast<Derived &*>(b1); // succeeds Derived d2 = dynamic_cast<Derived &*>(b2); // fails: exception thrown
class C {}; const C *a = new C; C *b = const_cast<C *>(a);