Casting in C++

黄舟
Release: 2017-02-06 13:43:52
Original
1878 people have browsed it

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;
Copy after login


Or:

i = int (d);
Copy after login


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)
Copy after login


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);
Copy after login


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);
Copy after login


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:


## Code:

double d = 3.14159265;
int i = static_cast<int>(d);
Copy after login


3 dynamic_cast


dynamic_cast is only used for pointers and references to objects. When used with polymorphic types, it allows arbitrary implicit type conversions and vice versa. However, unlike static_cast, in the latter case (note: the reverse process of implicit conversion), dynamic_cast will check whether the operation is valid. That is, it checks whether the conversion will return a valid complete object that was requested.


Detection occurs at runtime. If the converted pointer is not a valid complete object pointer as requested, the return value is NULL.


Code:

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 &#39;NULL&#39;
Copy after login


If a type conversion is performed on a reference type and this conversion is not possible, a bad_cast exception type is thrown:


Code:

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
Copy after login

4 const_cast


This conversion type manipulates the const attribute of the passed object, either setting or removing it:


Code:

class C {};
const C *a = new C;
 
C *b = const_cast<C *>(a);
Copy after login


The other three operators cannot modify the constancy of an object. Note: 'const_cast' can also change a type's volatile qualifier.


Each of the four cast forms of C++ is suitable for a specific purpose


dynamic_cast is mainly used for execution "Safe downcasting", that is, determining whether an object is a specific type in an inheritance hierarchy. It is the only cast that cannot be performed with the old-style syntax, and it is the only cast that may have a significant runtime cost.

static_cast can be used to force implicit conversions (for example, non-const objects are converted to const objects, int is converted to double, etc.), and it can also be used to reverse conversions of many such conversions ( For example, a void* pointer is converted to a typed pointer, and a base class pointer is converted to a derived class pointer), but it cannot convert a const object into a non-const object (only const_cast can do it), which is closest to C-style Convert.

const_cast is generally used to force the elimination of object constancy. It's the only C++-style cast that can do this.

reinterpret_cast is intended for low-level casts that lead to implementation-dependent (i.e., non-portable) results, such as converting a pointer to an integer. Such casts should be extremely rare outside of low-level code.

The above is the content of forced type conversion in C++. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template