C++--type conversion

1. What is type conversion

In the traditional C language, there are mandatory type conversion and implicit type conversion. The implicit type conversion is automatically processed by the compiler at the compilation stage, and it can be converted if it can be converted. The mandatory type conversion is converted by the user.

defect:

  • The visibility of conversion is relatively poor, all conversion forms are written in the same form, it is difficult to track wrong conversion
  • Implicit type conversion may cause problems in some cases: such as loss of data precision
  • Explicit type conversion mixes all cases together, code is not clear enough
     

for example:

    int a = 100;
    long long b = a;//implicit type conversion
    char c = b;//implicit type conversion
    size_t d = a;//implicit type conversion
    cout << a <<" " <<" " << b <<" " << c <<" " << d << endl;
    //forced type conversion
    double e = (double)a;
    float f = (double)e;
    cout << e << " " << f << endl;

In C++, the conversion method of C language can also be used, but in C++, there is its own conversion method,

In order to enhance the visibility of type conversion, standard C++ introduces four named cast operators:
static_cast, reinterpret_cast, const_cast, dynamic_cast

2. Different operator usage scenarios

2.1 static_cast usage

In static_cast, compared with the implicit type conversion in C language, its use method can only convert two types that are relatively close, and cannot be used for conversion of irrelevant types.

for example:

    int a = 100;
    char b= static_cast<char>(a);
    //char* c = static_cast<char*>(a);//无法转换
    cout << b << endl;

2.2 reinterpret_cast use

The reinterpret_cast operator generally provides a lower-level reinterpretation of the bit pattern of the operand, coercing two unrelated types, that is, one type is converted to another unrelated type,

for example:

    int a = 100;
    char b= static_cast<char>(a);
    char* c = static_cast<char>(b);// cannot be converted

    char* c = reinterpret_cast<char*>(b);
    printf("%c\n", *(&c));

2.3 const_cast use

The most common use of const_cast is to delete the const attribute of a variable to facilitate assignment, but it is necessary to use volatile to modify the variable with const added.

for example:

     volatile const int a = 10;
     int* p  =const_cast<int*>(&a);
     p = (int*)&a;
    *p = 20;
    cout << *p <<" " << a << endl;

2.4 How to use dynamic_cast

dynamic_cast is used to convert a pointer/reference of a parent class object to a pointer or reference of a subclass object (dynamic conversion)

Upward transformation: subclass object pointer/reference -> parent class pointer/reference (no conversion required, assignment compatible rules)

Downcast: parent class object pointer/reference -> subclass pointer/reference (it is safe to use dynamic_cast to transform)
Note:
1. dynamic_cast can only be used for classes with virtual functions in the parent class
2. dynamic_cast will first check whether it can be converted Success, convert if successful, return 0 if not,
for example:

class A
{
public:
    virtual void f() {}
};

class B : public A
{};

//The A* pointer may point to the parent class, or it may point to the subclass
void fun(A* pa)
{     // dynamic_cast will first check whether the conversion is successful, if it is successful, it will be converted, if not, it will return

    // If pa points to a subclass, it can be converted, and the conversion expression returns the correct address

    // If pa points to the parent class, it cannot be converted, and the conversion expression returns nullptr

    B* pb1 = static_cast<B*>(pa);
    B* pb2 = dynamic_cast<B*>(pa);//返回空
    cout << "pb1:" << pb1 << endl;

    cout << "pb2:" << pb2 << endl;
}
int main()
{

     //The parent class object cannot be converted to the subclass
    A a;
    B b;
    fun(&a);
    fun(&b);
    return 0;
}

 Note that
casts turn off or suspend normal type checking. Before using casts each time, programmers should carefully consider
whether other different ways to achieve the same purpose. If non-casts are not possible, they should limit the cast Convert the scope of the value
to reduce the chance of errors. Strong recommendation: Avoid using casts.

Guess you like

Origin blog.csdn.net/weixin_66828150/article/details/132327058