Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to use constant cast in C++, give a simple example.
How to use constant cast in C++, give a simple example.
The method of forced type conversion in C language is simple, as long as the type to be converted is determined by brackets before the variable to be converted. If you want to force a double variable to be converted to an int type, the code is as follows:

Double x = 3.14; int y =(int)x;

In addition, there can be more advanced conversion, such as converting variables of type int * into int. The code is as follows:

int x = 20int * p = & ampx; int y =(int)p;

In order to make the forced type conversion in C++ language more obvious and safe, the forced type conversion in different situations is divided into four types, namely:

Static_cast static type conversion is the safest forced type conversion.

Const_cast converts between mutable and immutable types.

Dynamic_cast is the transformation between parent class and subclass in polymorphic case.

Conversion between all types, the most unsafe conversion.

Static_cast in 1 C++ performs non-polymorphic conversion instead of the usual conversion operation in C. Therefore, it is used as implicit type conversion. For example:

int I;

Floating point f =166.7f;

I = static _ cast & ltint & gt(f) and:

As a result, the value of I is 166.

2. reinterpret_cast in 2.c+++mainly converts data from one type to another. The so-called "usually providing a lower level of reinterpretation for the bit pattern of operands" means reinterpreting data in the form of binary existence. For example:

int I;

Char *p = "This is an example." ;

i = reinterpret _ cast & ltint & gt(p);

As a result, the values of I and P are exactly the same. The function of reinterpret_cast is to interpret the value of pointer P as an integer in binary (bit pattern), and assign it to I. //i is also a pointer and an integer pointer. An obvious phenomenon is that there is no digital loss before and after conversion.

Therefore, this transformation can also be called "retranslation"

3.dynamic_cast is mainly used for uplink and downlink conversion between classes, and can also be used for cross conversion between classes.

Dynamic_cast and static_cast have the same effect when performing uplink conversion between class levels;

In downlink conversion, dynamic_cast has the function of type checking, which is more secure than static_cast.

Class b {

Public:

int m _ iNum

Virtual void foo ();

};

Class d: public B{

Public:

char * m _ SZ name[ 100];

};

Invalid function (B *pb)

{

d * PD 1 = static _ cast & lt; D * & gt(Pb);

D * pd2 = dynamic _ cast & ltD * & gt(Pb);

}

In the above code segment, if pb points to an object of type D, pd 1 and pd2 are the same, and it is safe to perform any operation of type D on these two pointers;

However, if pb points to an object of type B, then pd 1 will be a pointer to the object, and it will be unsafe to perform D-type operations on it (such as accessing m_szName), while pd2 will be a null pointer.

4.Const_cast This operator is used to modify the Const or volatile property of a type. Except for const or volatile decoration, type_id and expression are the same type.

1.const pointer is converted into a non-constant pointer and still points to the original object;

Second, constant reference is converted into non-constant reference, which still points to the original object;

3. Constant objects are converted into non-constant objects.

Voiatile and const class tests. Give the following examples:

Class b

{

Public:

int m _ iNum

B(){}

};

void foo()

{

const B B 1;

//b 1 . m _ iNum = 100; //Compilation error

B& amp; b2 = const _ cast & ltB& amp; & gt(b 1);

b2。 M _ iNum = 200// fine?

}

int main()

{

foo();

Returns 0;

}

The above code will report an error when compiling, because b 1 is a constant object and cannot be changed;

Use const_cast to convert it into an invariant object, and its data members can be changed at will. Note: b 1 and b2 are two different objects.