Four types of conversions in C + +

Source: Internet
Author: User

reinterpret_cast< Target type > (original type variable)//re-interpret type conversion dynamic_cast<new_type> (expression)//dynamic type conversion static_cast<new _type> (expression)//static type conversion const_cast<new_type> (expression)//returns a pointer to a very amount


1.const_cast: A constant pointer is converted to a very good pointer, and still points to the original object, and the constant reference is converted to a very good reference, and still points to the original object;

    const int a = +;    Const int* P1 = &a;    *P1 = 200; ERROR    int* p2 = const_cast<int*> (p1);    *P2 = 200; Ok
   Class a{};   Const a *a = new A;   A *b= const_cast<a*> (a);   A &c= const_cast<a&> (*a);

2. Dynamic type conversion: Used in a polymorphic parent-child class pointer or reference. Dynamic type conversions are checked at the angle of the object pointed to by the pointer, but the compilation is not error-free, but in the execution phase, it will fail with a null pointer, if it is a reference, throw an exception, be caught by the system and then kill the process, which is the safest form of conversion.


3. Static type conversions: If implicit type conversions can be made in one direction between the target type and the source type, then a static type conversion can be done in two directions. Conversely, if the implicit type conversion cannot be done in two directions, then the static type conversion cannot be done in either direction. Static type conversions are checked at the angle of the pointer type, which is done during the compile phase.


4. Re-interpreting Type conversions: type conversions between pointers or references of different types, and type conversions between pointers and integers. No type checking is performed either at compile time or in the execution phase, and it is only the responsibility to turn one type into another type, which is the most dangerous form of conversion.

#include <iostream>using namespace Std;class a {virtual void foo (void) {}};class b:public A {};class c:public b {};class D {};int main (void) {b b; * pa = &b;cout << pa << endl;cout << "--------dynamic DC--------" << endl;//pa points to B object, so b* PB is successful = Dynamic_cast<b*> (PA); cout << PB << endl;//pa no point to C object, fail, secure c* pc = Dynamic_cast<c*> (PA); cout &L t;< pc << Endl; a& RA = b;try {c& rc = dynamic_cast<c&> (RA);} catch (exception& ex) {cout << "type conversion failed:" << ex.what () << endl;//...} PA does not point to D object, fail, security d* PD = Dynamic_cast<d*> (PA), cout << PD << endl;cout << "--------Static conversion sc------ --"<< endl;//B is a subclass of a, so successful PB = Static_cast<b*> (PA); cout << PB << endl;//C is a grandson class, success, Danger! PC = Static_cast<c*> (PA); cout << pc << endl;//D is not a descendant of a, failure, security//pd = static_cast<d*> (PA);//cout &L t;< PD << endl;cout << "--------re-interpreting RC--------"<< endl;//no checks at compile time or run time, Danger! PB = Reinterpret_cast<b*> (PA), cout << pb << endl;pc = reinterpret_cast<c*> (PA); cout << pc << ENDL;PD = reinterpret_cast<d*> (PA); cout << PD << Endl;return 0;}



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

Four types of conversions in 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.