4 New Types of conversions in C ++ and 5. c4 types of conversions

Source: Internet
Author: User

4 New Types of conversions in C ++ and 5. c4 types of conversions

1. First, let's review the forced conversion of C.

As we all know, when compiling a forced conversion in C language, the compiler will not check whether the conversion is successful, and the compilation will be correct.

For example:

#include "stdio.h"struct Position{int x;int y;};int main(){ int i; struct Position *p; i=0x123456; p=(struct Position *)i; printf("px=%d,py=%d\n",p->x,p->y);}

Shows the output result:

 

It can be seen that the segment error occurs only when the code is run.

When C codeThousands of linesIf this problem occurs, it is very difficult to find.

 

2. New type conversion of C ++

Therefore, in C ++4TypeForced type conversion

2.1 static_cast (static type conversion)

  • UsedBasic DataConversions between types (char, int, const int, etc)
  • Cannot be usedConversion between basic data type pointers (char *, int *, etc)
  • UsedClass objects with inheritance relationshipsConversion
  • UsedClass pointerConversion

For example:

Int I = 0x45; char c = 'C'; c = static_cast <char> (I); // char * pc = static_cast <char *> (& I ); // This row is incorrect and cannot be used for conversion between basic pointers

 

 

2.2 const_cast (non-constant type conversion)

  • UsedRemoveVariableRead-only attribute
  • And the forced conversion type must bePointer*OrReference&

For example:

Const int x = 1; // const: defines a constant x const int & j = 2; // const reference: define a read-only variable jint & p1 = const_cast <int &> (x); // forcibly convert int & int * p2 = const_cast <int *> (& j ); // force convert int * // int p3 = const_cast <int> (j); // This row is incorrect. The normal data type p1 = 3 cannot be converted; * p2 = 4; printf ("x = % d, j = % d \ n", x, j); printf ("p1 = % d * p2 = % d \ n", p1, * p2 );

Output result:

x=1   j=4p1=3  *p2=4

From the output result, we can see that only j content is changed when p1 and p2 are modified. It is a read-only variable because the variable j is referenced and defined by const.

 

2.3 dynamic_cast (dynamic type conversion)

  • UsedClass pointers with inheritance relationshipsInter-Domain Conversion
  • UsedCross-correlation class pointerInter-Domain Conversion
  • WithType checkFunctions
  • YesVirtual FunctionsSupport
  • Cannot be usedConversion between basic data type pointers (char *, int *, etc)

 

2.4 reinterpret _ cast (Interpretation type conversion)

  • UsedAll pointersForced conversion

(Interpretation refers to re-interpreting the data to be converted)

For example:

Int I = 0; char j = 'C'; int * p1 = reinterpret_cast <int *> (& I); char * p2 = reinterpret_cast <char *> (& j ); // int p3 = reinterpret_cast <int> I; // This row is incorrect. Normal data type cannot be converted.

 

 

 

 

 

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.