Coercion type conversions for C + +

Source: Internet
Author: User

In C and C + +, coercion of type conversions often occurs, for example:

void Test () {int i = 1;     Implicitly-typed conversions double d = i;     printf ("%d,%.2f\n", I, D); int* p = &i;     Force type conversion int address = (int) p; printf ("%x,%d\n", p, address);}

Therefore, forced type conversions often exist, so C + + style type conversion provides 4 types of conversion operators to handle different situations of application. They are const_cast,static_cast,dynamic_cast,reinterpreter_cast, respectively.


The reinterpret_cast operator is used to convert one type to another different type. For example

typedef void (* FUNC) (); int dosomething (int i) {cout<< "dosomething" <<endl; return 0;}

Reinterpret_cast can be the compiler in the definition of Func to see the dosomething function, so the very bug, the following conversion function pointer code is not portable, so it is not recommended, and C + + does not guarantee that all function pointers are used the same way, So the use of this sometimes produces indeterminate results.

void Test () {FUNC f = reinterpret_cast< func> (dosomething); f ();}


The most common use of const_cast is to remove the const attribute of a variable to make it easy to assign a value.

void Test () {volatile const int tem = 3;     int* p = const_cast< int*> (&tem);     *p = 8; Cout<<tem<<endl;}

dynamic_cast A pointer or reference for converting a pointer of a subclass object to a parent class object (dynamic conversion)

Move Up: Parent-class pointer/reference with subclass object (no conversion required)

Down Transformation: Parent object, subclass pointer/reference (transformation with dynamic_cast is safe)

I. dynamic_cast can only be used for classes that contain virtual functions

Two. The dynamic_cast will first check if the conversion succeeds, the conversion succeeds, and the return of 0 is not possible.

Class a{public:virtual void Test () {}};class b:public a{};void fun (* pa) {b* PB1 = static_cast<b*> (PA)   ;      b* PB2 = dynamic_cast<b*> (PA);   cout<< "PB1:" <<pb1<< Endl; cout<< "PB2:" <<pb2<< Endl;}


static_cast is similar to a C-style cast. Unconditional conversion, static type conversion. For:

1. Conversion between a base class and a subclass where a child class pointer is safe to convert to a parent pointer, but it is unsafe for a parent pointer to be converted to a child class pointer. (Dynamic type conversion between base class and subclass recommended with dynamic_cast)

2. Basic data type conversions. enum, struct, int, char, float, and so on. static_cast cannot perform conversions between unrelated types (such as non-base classes and subclasses).


3. Convert the null pointer to a null pointer of the target type.


4. Convert any type of expression to void type.


5. static_cast cannot remove the const, Volitale attribute of the type (with Const_cast).

Above is the analysis of four kinds of functions, if there is insufficient, please advise

Coercion type conversions for C + +

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.