Four types of coercion in C++

The four types of casts in C++ are: const_cast, static_cast, dynamic_cast, reinterpret_cast; this is a very common topic in C++, especially for newcomers. Then...
Generally speaking, the following situations will occur in type conversion:
-int conversion float: often appears in mathematical calculations, such as: improving the accuracy of calculations;
-int conversion enum: enumerations are often used for ideograms, The actual calculation is still the int type;
-int and pointer conversion: When trying to calculate the location of a member variable in the memory space of the object based on the offset bit, the pointer needs to be converted to an integer for calculation. When the position of the variable (integer type) is calculated, it needs to be converted to a pointer type.
-Typed pointers and untyped pointers:
-Converting data of different lengths: the problem of intercepting data;
-Class pointer conversion with inheritance: mostly used in polymorphic scenarios;
-Class pointer conversion without inheritance: not particularly common

When testing the above conditions, we will have the following problems:
1. Compiler error. This is because the grammar stipulates that this use is illegal. Therefore, when the compiler compiles the code, it considers this behavior illegal and terminates the subsequent process.
2. An error occurred during operation. This is because it is grammatically legal, but it is not reasonable at runtime.

Below, we mainly talk about the difference between const_cast, static_cast, dynamic_cast, and reinterpret_cast four types of coercion:

1. static_cast

Is the most commonly used type of conversion operator, as shown below:

int i;
float f = 3.28;
i = static_cast<int>(f);

2. const_cast

Seeing that const should not be regarded as quantifying its length, but on the contrary; its function is to take out the const attribute of the variable and turn the pointer of const type into pointer of non-const type; as shown below:

const int *fun(int a, int b){}
int const_cast<int *> (fun(1, 2));

3. dynamic_cast

It is mainly used to check whether the type conversion complies with type safety at runtime, but it is only legal when used in polymorphic types, that is, there is at least one virtual in the class.

4. reinterpret_cast

The Chinese meaning of reinterpret means "re-interpret"; while the binary form of the data in the hot interpret_cast is re-interpreted, but the binary value will not be changed. As follows:

int i;
char *ptr = "I Love You!!!";
int i = reinterpret_cast<int>(ptr);

The following code replaces the previous cases:

//指针转换整型
int a = dynamic_cast<int>(int *p);
//整型转换指针
int *p = dynamic_cast<int *>(int a);

//整型转换浮点型
float a = dynamic_cast<float>(int a);
//浮点型转换整型
int a = dynamic_cast<int>(float);

//类型指针转换无类型指针
void *p = dynamic_cast<void *>(int *p1);
//无类型指针转换类型指针
int *p = dynamic_cast<int *>(void *p);

//整型转换枚举
TYPE t = dynamic_cast<TYPE>(int a);
//枚举转换整型
int a = dynamic_cast<int>(TYPE t);

//无类型指针转换整型
int a = dynamic_cast<int>(void *p);
//无类型指针转换其他指针
Other *p = dynamic_cast<Other *>(void *pv);

Guess you like

Origin blog.csdn.net/CFH1021/article/details/73743814