Differences between static_cast < Type-id > (expression) and reinterpret_cast<type-id> (expression)

Source: Internet
Author: User

static_cast< Type-id > (expression) with the reinterpret_cast<type-id> (expression) the Difference


1. static_cast < Type-id > (expression)

the operator puts expression converted to Type-id type, but there is no run-time type check to guarantee the security of the conversion. It is mainly used in the following ways:

① used to class Hierarchy the base class (parent class) and Derived Classes (subclass) A pointer or reference conversion.

It is safe to make an upstream conversion (to convert a pointer or reference from a derived class to a base class representation).

When you make a downstream conversion (a base class pointer or reference is converted to a derived class representation), it is unsafe because there is no dynamic type checking.

② used for conversions between basic data types, such as the int converted into Char , put int converted into enum . The security of this conversion is also to be ensured by the developer. static_cast<> () can be used wherever implicit type conversions are available in the 2.1 C language to convert

③ convert a null pointer to a null pointer of the target type?

For example:

double da = 1.1;

void *pa =&da;

double *DP =static_cast<double*> (PA);

double *ddp= reinterpret_cast<double*> (PA);

int ia = static_cast<int> (DA);

cout << *DP << Endl;

cout<<*ddp<<endl;

cout << ia << Endl;

Output Result:

④ Convert any type of expression into an void type

For example char* pa= "ABCD"; void * pb=static_cast<void*> PA;

2. reinterpret_cast<type-id> (expression)

Type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer. It can convert a pointer to an integer, or it can convert an integer to a pointer (a pointer is converted to an integer, the integer is converted to the original type of pointer, and the previous pointer value can be obtained).

operator modified the Number of operands type , but simply to re-interpret the given object's bit model without Binary Conversions .

( 1 ) static_cast<> () static type conversions, compile-time C + + the compiler does type checking, the base type can be converted, but the pointer type cannot be converted.

( 2 If the type is forced to convert between different types, use the reinterpret<> () to be re-interpreted

( 3 ) General conclusions: C in Language that can be implicitly type-converted, in C + + Available in static_cast<> () for type conversion. Because the C + + compiler is generally able to pass the compile check

C The language cannot be converted with an implicit type, C + + can be used in reinterpret_cast<> () for mandatory type interpretation.

For example:

Example 1 :

int *n= new int ;

*n=100;

double *d=reinterpret_cast<double*> (n);

double dd=static_cast<double> (*n);

cout<<*n<<endl;

cout<<*d<<endl;

cout<<dd<<endl;

Operation Result:

Example 2:

Char* p1="ABCD";

int *p2=reinterpret_cast<int*> (p1);

int *p5= (int*) p1;

cout<<p1<<endl;

cout<<p2<<endl;

cout<<p5<<endl;

Operation Result:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Differences between static_cast < Type-id > (expression) and reinterpret_cast<type-id> (expression)

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.