Casting in C++
Feb 06, 2017 pm 01:43 PMQ: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
1 2 3 |
|
Or:
1 |
|
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:
1 2 3 4 |
|
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:
1 2 3 4 |
|
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:
1 2 3 4 5 |
|
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:
1 2 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 |
|

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Concurrency-safe design of data structures in C++ concurrent programming?

How to implement a custom comparator in C++ STL?

C++ object layout is aligned with memory to optimize memory usage efficiency

How to implement the Strategy Design Pattern in C++?

Similarities and Differences between Golang and C++

How to implement C++ multi-thread programming based on the Actor model?

What are the underlying implementation principles of C++ smart pointers?
