How to use four types of conversion operators in C ++

Source: Internet
Author: User
The four types of conversion operators in C ++ have been around for a long time, but they have never been quite clear about their usage. Today, I saw an explanation in a book to get a general idea of their specific usage.

The details are summarized as follows:

  Reinterpret_cast

This function converts a pointer of one type to a pointer of another type.

In this conversion, you do not need to modify the format of the pointer variable value (without changing the pointer variable value). You only need to re-interpret the pointer type during compilation.

Reinterpret_cast can convert the pointer value to an integer, but it cannot be used for non-pointer type conversion.

Example:

// Type conversion of basic type pointers

Double d = 9.2;

Double * pd = & d;

Int * pi = reinterpret_cast (pd); // equivalent to int * pi = (int *) pd;

// Type conversion of irrelevant class pointers

Class {};

Class B {};

A * pa = new;

B * pb = reinterpret_cast (pa); // equivalent to B * pb = (B *) pa;

// Convert the pointer to an integer

Long l = reinterpret_cast (pi); // equivalent to long l = (long) pi;

  Const_cast

This function is used to remove the constant attribute of the pointer variable and convert it into a common variable corresponding to the pointer type. In turn, you can also convert a very large number of pointer variables into a constant pointer variable.

This type of conversion is made during compilation.

Example:

Const int * pci = 0;

Int * pk = const_cast (pci); // equivalent to int * pk = (int *) pci;

Const A * pca = new;

A * pa = const_cast (pca); // equivalent to A * pa = (A *) pca;

For security reasons, const_cast cannot convert non-pointer constants to common variables.

  Static_cast

This function is mainly used to convert between basic types and types with inheritance relationships.

This conversion usually changes the internal representation of the variable. Therefore, applying static_cast to pointer type conversion does not make much sense.

Example:

// Basic type conversion

Int I = 0;

Double d = static_cast (I); // equivalent to double d = (double) I;

// Convert the inherited Class Object to the Base Class Object

Class Base {};

Class Derived: public Base {};

Derived d;

Base B = static_cast (d); // equivalent to Base B = (Base) d;

  Dynamic_cast

It is a dynamic conversion opposite to static_cast.

This type of conversion analysis is performed at runtime rather than during compilation, which is significantly different from the preceding three types of conversion operations.

This function can only perform type conversion between pointers of inherited class objects or between references. During conversion, the conversion between type objects is determined based on the current running type information. The pointer conversion of dynamic_cast fails. Check whether the value is null. If the conversion fails to be referenced, A bad_cast exception is thrown.

Example:

Class Base {};

Class Derived: public Base {};

// Convert a pointer from a derived class to a base class pointer

Derived * pd = new Derived;

Base * pb = dynamic_cast (pd );

If (! Pb)

Cout "type conversion failed"> endl;

// There is no inheritance relationship, but the converted class has a virtual function

Class A (virtual ~ A ();) // has A virtual function

Class B {}:

A * pa = new;

B * pb = dynamic_cast (pa );

If object pointers without inheritance or virtual functions are converted, basic type pointers are converted, and base class pointers are converted to derived class pointers, they cannot be compiled.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.