4 Types of transformation in C + + the detailed parsing _c language

Source: Internet
Author: User
Tags constant inheritance

The specific summary is as follows:

(1) reinterpret_cast
This function converts a pointer of one type to a pointer of another type.
This conversion does not change the format of the pointer variable value (without changing the value of the pointer variable), it is only possible to redefine the type of the pointer at compile time.
Reinterpret_cast can convert a pointer value to an integer, but not to a cast that is not a pointer type.
Cases:
Type conversions for base type pointers
Double d=9.2;
double* PD = &d;
int *pi = reinterpret_cast<int*> (PD); equivalent to int *pi = (int*) PD;

Type conversions of pointers to unrelated classes
Class a{};
Class b{};
A * pa = new A;
b* PB = reinterpret_cast<b*> (PA); Equivalent to b* PB = (b*) pa;

The pointer converts to an integer
Long L = reinterpret_cast<long> (pi); Equivalent to Long L = (long) pi;

(2) Const_cast
This function is used to remove the constant property of a pointer variable and convert it to a generic variable of the corresponding pointer type. Conversely, you can also convert a very variable pointer to a constant pointer variable.
This conversion is a type change made during compilation.
Cases:
Const int* PCI = 0;
int* PK = const_cast<int*> (PCI); Equivalent to int* PK = (int*) PCI;

Const A * PCA = new A;
A * PA = const_cast<a*> (PCA); Equivalent to A * PA = (A *) PCA;

For security reasons, const_cast cannot convert a constant that is not a pointer to a normal variable.

(3) static_cast
This function is primarily used for conversions between basic types and types that have inheritance relationships.
This transformation generally changes the internal representation of a variable, so it doesn't make much sense for static_cast to be applied to pointer type conversions.
Cases:
Basic Type Conversions
int i=0;
Double d = static_cast<double> (i); Equivalent to Double d = (double) i;

To convert an object of an inheriting class to a base class object
Class base{};
Class Derived:public base{};
Derived D;
Base B = static_cast<base> (d); Equivalent to base b = (base) D;

(4) dynamic_cast
It is relative to the static_cast and is dynamically transformed.
This conversion is analyzed at run time, not at compile time, and is significantly different from the above three type conversion operations.
The function can only type conversions between pointers to inherited class objects or between references. The conversion is based on the current Run-time type information to determine whether the conversion between the type objects is legitimate. The dynamic_cast pointer conversion failed, and a Bad_cast exception was thrown if the reference conversion failed, either by null detection or not.
Cases:
Class base{};
Class Derived:public base{};

The derived class pointer converts to a base class pointer
Derived *pd = new Derived;
Base *PB = dynamic_cast<base*> (PD);

if (!PB)
cout << "type conversion failure" << Endl;

There are no inheritance relationships, but the converted classes have virtual functions
Class A (Virtual ~a ();)//virtual function
Class b{}:
A * pa = new A;
b* PB = dynamic_cast<b*> (PA);

If you convert to an object pointer without an inheritance relationship or a virtual function, base type pointer conversion, and a base class pointer to a derived class pointer, you cannot compile it.

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.