C + + Explicit type conversion sample detailed _c language

Source: Internet
Author: User
Tags volatile

Standard C + + contains an explicit conversion syntax:

Static_cast: For "benign" and "moderately benign" conversions, including the need not to cast

Const_cast: For "Const" and/or "volatile" for conversion

Reinterpret_cast: Conversion to a completely different meaning. In order to use it safely, the key must be converted back to the original type. The converted type is generally used only for bitwise operations, otherwise it is for other covert purposes. This is the most dangerous of all conversions.

dynamic_cast: For type-safe downward conversion


Static_cast: For all explicitly defined transformations, including void* casts, implicit type conversions, and static positioning of class hierarchies.

1. Conversion of data types from small to large, such as int to long or float;

Copy Code code as follows:

int i; Static_cast<long> (i); Static_cast<float> (i);

2. Narrowing conversion, that is, the conversion of data types from large to small, may lose data;

3. C + + is not allowed to assign a value to a void* type of data;

Copy Code code as follows:

void* ptr; ptr = static_cast<void*> (i);

Const_cast: Constant conversions, you can use Const_cast if you convert from const to non-const or from volatile to non-volatile. This is the only conversion allowed by const_cast.
Copy Code code as follows:

volatile int k = 0; int* u = const_cast<int*> (&k);

four explicit type conversion usage examples

1, reinterpret_cast
A type conversion function converts a pointer of one type to a pointer of another type. This conversion does not need to modify the data storage format of the pointer variable value, just recompile the type of the interpretation pointer at compile time.

Copy Code code as follows:

Double d=9.3;
Double *pd=&d;
int *pi=reinterpret_cast<int*> (PD);

However, it cannot be used for conversions that are not pointer types.
As with an implicit conversion, reinterpret_cast cannot convert a const pointer to a void* pointer.

2, Const_cast
The constant property that is used to remove the pointer variable and converts it to a generic variable of the corresponding pointer type. You can also convert a pointer variable of a very magnitude to a constant pointer variable, making a type change during compilation.

Copy Code code as follows:

Const int* pci=0;
int* pj=const_cast<int*> (PCI);

Based on security considerations, Const_cast cannot convert a constant variable that is not a pointer to a normal variable.
You can convert the normal pointer variable pi to a constant pointer variable, but you cannot convert a generic variable that is not a pointer to a constant variable.

3, static_cast
For conversions between basic types and types that have inheritance relationships, this transformation typically changes the internal representation of a variable. For pointer type conversions, it doesn't make much sense.

Copy Code code as follows:

Class Base ();
Class Derived:public base{}
Derived D;
Base d=static_cast<base> (d);

You can convert an inherited class object to a base class object. But not in the reverse.
Note: The base class pointer converts to an inherited class pointer, in some danger.

4, dynamic_cast
Compared with static_cast, it is a dynamic dynamic_cast conversion. This conversion is analyzed at run time, not at compile time. Type conversions can only be made between pointers to inherited class objects or between references. The conversion is based on the current (RTTI) decision as to whether the conversion between the type objects is legitimate. The dynamic_cast conversion fails, is detected by a null pointer, the reference conversion fails, and the Bad_cast exception is thrown.
Converting an inherited class pointer or reference to a base class pointer or reference can, in turn, be a bad thing, but it can also be done if there are virtual functions in the base class, which means that the converted class has a virtual function object pointer.

Copy Code code as follows:

Class Base ();
Class Derived:public base{}
Derived *pd=new Derived;
Base *d=dynamic_cast<base*> (PD);

In addition, if there is no inheritance relationship, but the converted class has an object pointer to a virtual function, the conversion can also be compiled through.
Copy Code code as follows:

int i;
Long m;
M=static_cast<long> (i);
const int i=0;
int* j= (int*) &i;
J=const_cast<int*> (&i)//Convert the const to volatile.

Copy Code code as follows:

#include "iostream"
using namespace Std;
const int sz=100;
struct X
int A[sz];
void print (x* X)
for (int i=0;i<sz;i++)
cout<<x->a[i]<< ';
cout<<endl<< "------------" <<endl;
int main ()
x x;
Print (&x);
int* xp=reinterpret_cast<int*> (&x);//cast &x to int*
for (int* i=xp;i<xp+sz;i++)
Cannot use XP as a x* at this point usless you cast it BAKC;
Print (reinterpret_cast<x*> (XP));
Print (&x);
return 1;

Related Article

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.