Introduction to cast type conversion in C++

Introduction to cast type conversion in C++

In C++, cast is a syntax for converting between mismatched data types. It provides a convenient way to explicitly convert one data type to another. It should be noted that type conversion may cause data precision loss or overflow, so you need to be careful when using it.

C++ provides two styles of casts: C-style casts and C++-style casts.

★C++ provides two types of casts:

★C style cast: use parentheses notation, for example:

int x = 10; 

double y = (double)x; // Convert x to double type

★C++ style cast, providing four cast operators:

☆static_cast: static type conversion. It can convert between related types, such as converting a base class pointer to a derived class pointer, or converting an integer to a floating point number. static_cast does not require runtime type information support. This is a C++ type conversion operator, and its syntax format is:

static_cast<target type>(value to be converted)

Among them, the target type represents the type to which the value to be converted is converted. The value to be converted can be any type that is implicitly convertible to the target type.

For example:

int x = 10; 

double y = static_cast<double>(x); // Convert x to double type

☆reinterpret_cast: Reinterpret type conversion. It is mainly used to convert base class pointers to pointers of unrelated types, such as converting base class pointers to void* pointers. This conversion may cause runtime errors because it may violate type safety. reinterpret_cast does not require runtime type information support. For example:

int a = 10; 

int* p = &a; 

char* c = reinterpret_cast<char*>(p); // Convert pointer to char* type

☆const_cast: Constant type conversion. It is used to convert a constant object to a non-constant object, or a non-constant object to a constant object. const_cast does not require runtime type information support. For example:

const int a = 10; 

int* p = const_cast<int*>(&a); // Remove const attributes 

*p = 20; // The value of a can be modified

☆dynamic_cast: dynamic type conversion. It is mainly used for up and down conversions in polymorphic types, such as converting a base class pointer to a derived class pointer. dynamic_cast requires runtime type information support, so when the compiler does not enable runtime type information, it may cause program errors. For example:

class Base {}; 

class Derived : public Base {}; 

Base* b = new Derived; // Upward transformation 

Derived* d = dynamic_cast<Derived*>(b); // Dynamic type conversion 

if (d != nullptr) { 

    // Conversion successful 

} else { 

    // Conversion failed 

}

It should be noted that forced type conversion may bring certain risks, such as type mismatch, overflow, and undefined behavior, so you should ensure that the conversion is reasonable when using it.

Here is a complete example of converting a variable of type double to a variable of type int:

#include <iostream>
using namespace std;

int main() {
    double d = 3.1415926535897;
    int i = (int)d;   // C风格将double类型的d转换成int类型的i
    int j = static_cast<int>(d);   // C++风格将double类型的d转换成int类型的j
    cout << "i = " << i << endl;   // 输出转换后的值i:3
    cout << "j = " << j << endl;   // 输出转换后的值j:3

    return 0;
}

Guess you like

Origin blog.csdn.net/cnds123/article/details/132635358