C ++ display Conversion

Source: Internet
Author: User

C ++ display Conversion

Display conversion makes it easy for us to find them, because we can find them by name:

Static_cast For "benign" and "moderate benign" conversions, including not forced conversions
Const_cast Convert "const" and "volatile"
Reinterpret_cast To use it securely, The key must be converted to the original type. This is the most dangerous of all conversions
Dynamic_cast Type-safe downward Conversion

1. static conversion (static_cast)

Static_cast includes typical unforced transformations, narrow (information loss) transformations, forced conversions using void *, implicit type transformations, and static class-level positioning.

Usage: static_cast <type-id> (expression)

This operator converts expression to the type-id type, but does not check the runtime type to ensure the conversion security. It has the following usage:
① It is used to convert pointers or references between classes and subclasses in the class hierarchy.
It is safe to perform upstream conversion (converting the pointer or reference of a subclass to a base class;
When performing a downstream conversion (converting a base class pointer or reference to a subclass), it is not safe because there is no dynamic type check.
② It is used for conversion between basic data types. For example, convert int to char and convert int to enum. The security of such conversions must also be ensured by developers.
③ Convert a null pointer to a pointer of the target type.

Void func (int ){}

Int main ()

{

Int I = 0x7fff;

Long l;

Float f;

 

// Typical unforced type conversion

L = I;

F = I;

 

L = static_cast <long> (I );

F = static_cast <float> (I );

 

// Narrow Transformation

I = l;

I = f;

I = static_cast <int> (l );

I = static_cast <int> (f );

Char c = static_cast <char> (I );

 

// Use the forced conversion of void *

Void * vp = & I;

Float * fp = (float *) vp;

 

// Error: In C ++, values cannot be assigned from void * without forced conversion.

// Fp = vp;

 

Fp = static_cast <float *> (vp );

 

// Implicit type conversion

Double d = 0.0;

Int x = d;

 

X = static_cast <int> (d );

Func (d );

Func (static_cast <int> (d ));

Return 0;

}

 

2. Constant Transformation (const_cast)

Usage: const_cast <type_id> (expression)
This operator is used to modify the const or volatile attributes of the type. In addition to const or volatile modification, type_id and expression are of the same type.
Constant pointers are converted to non-constant pointers and still point to the original object;
Constant reference is converted to a non-constant reference and still points to the original object;

 

If the conversion from const to non-const, from volatile to non-volatile, or from non-const to const, or from non-volatile to volatile, you can use const_cast.

 

Int main ()

{

Const int I = 0;

// Error: gets the const Object Pointer. It cannot be assigned to a non-const pointer without conversion.

// Int * j = & I;

 

Int * j = (int *) & I;

J = const_cast <int *> (& I );

// Error: In const_cast <type_id> (expression), The type_id and expression types are the same except for the const or volatile modifier.

// Long * l = const_cast <long *> (& I );

 

Volatile int k = 0;

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

}

 

3. reinterpret_cast)

Usage: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 an integer to a pointer (first, convert a pointer to an integer, and then convert the integer to the original type of pointer, you can also get the original pointer value ).

This is the most insecure conversion mechanism. Reinterpret_cast impersonates an object as another object of all different types. The reinterpret_cast idea is that when you need to use it, what you get is already different, so that it cannot be used for the original purpose of the type unless you convert it back again.

# 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); // The output is meaningless.

 

Int * xp = reinterpret_cast <int *> (& x );

For (int * I = xp; I <xp + sz; I ++)

{

* I = 0;

}

Print (reinterpret_cast <X *> (xp ));

Print (& x );

}


What is the difference between explicit and implicit conversions in C and C ++?

C type conversion: (target_type) (value), such as int I = 5; char c = (char) (I );
C ++ type conversion: There are four types
Static_cast type conversion similar to C Language
Direct reference and direct pointer conversion in the dynamic_cast Inheritance System
Const_cast constant conversion, constant conversion to constant, constant conversion to constant
Reintepret_cast re-interpret the conversion and re-interpret the meaning of the value, such as converting int to char *.

For type conversion in C language

Float;
A = 3.74
And
Float;
A = (float) 3.74.
Is equivalent.

The so-called implicit type conversion means that when data with low precision is assigned to a high precision
The data is automatically converted into smaller Precision values, for example:
Int a = 3;
Float B;
B =;
When B = a is executed, the value 3 of a is automatically converted to 3 of the floating point.

The so-called explicit type conversion means that when the data with high precision is assigned to the data with low precision
The data to be displayed is converted as follows:
Int;
Float B = 3.23;
A = (int) B;
When a = (int) B is executed, B is converted to an integer and assigned to.

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.