Home  >  Article  >  Backend Development  >  How to convert C++ types

How to convert C++ types

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-28 14:54:502648browse

Methods: 1. static_cast, used for conversion of non-polymorphic types; 2. reinterpret_cast, converted to another unrelated type; 3. const_cast, delete the const attribute of the variable and then assign it; 4. dynamic_cast , used for pointer or reference conversion between class inheritance hierarchies.

How to convert C++ types

The operating environment of this tutorial: Windows 7 system, C 17 version, Dell G3 computer.

How to convert C++ types

C In order to standardize type conversion in C and enhance the visibility of type conversion, four forced type conversion operators are introduced: static_cast, reinterpret_cast, const_cast , dynamic_cast

They are essentially template classes.

The following are introduced separately:

1.static_cast

It is used for non-polymorphic type conversion (static conversion), corresponds to Implicit type conversion in C,but it cannot be used for conversion of two unrelated types, such as conversion between integer and integer pointer, although both are four bytes , but one of them represents data and the other represents address. The types are irrelevant and cannot be converted.

This conversion is completed at compile time, similar to C-style type conversion, but please pay attention to the following points

Cannot convert between two class types that have no derivation relationship

Cannot remove the type modifiers of the original type, such as const, volatile, __unaligned

Since there is no dynamic type checking when converting objects, there are security risks when converting base class objects into derived class objects

void Test()
{
    //C中的方式
    int i = 10;
    double d1 = i;//隐式类型转换
    //int *p = i;//无法隐式类型转换,只能强制类型转换
    int *p = (int*)i;
    //C++中的方式
    double d2 = static_cast<double>(i);
    //相当于创建一个static_cast<double>类型的匿名对象赋值给d2
    int* p2 = static_cast<int*>(i);//无法转换,会报错
}

How to convert C++ types

2.reinterpret_cast

The meaning of reinterpret is to reinterpret, which can convert one type into another unrelated type ,Corresponds to forced type conversion in C, dealing with situations where implicit conversion cannot be performed

void Test()
{
    int i = 10;
    int* p2 = reinterpret_cast<int*>(i);
}

Forced type conversion can sometimes deal with some problems violently

For example:

For a function with parameters, how can I call the function without passing parameters?

void Fun(int s)
{
    cout << s << endl;
}
typedef void(*FUNC)();
void Test()
{
    FUNC pf = reinterpret_cast<FUNC>(Fun);
    pf();
}

Casting in C can also be handled.

Although we convert function pointers through this BUG method, such code is not portable and sometimes produces uncertain results, so it is not recommended to use it like this

As here The output value of s is a random value. Although the user does not pass external parameters, the function will create a formal parameter when it is called. The formal parameter is not initialized and is naturally a random value

How to convert C++ types

3.const_cast

Its function is to delete the const attribute of the variable to facilitate reassignment

This conversion is completed at compile time and is used to remove const and volatile modifiers. It can only convert pointers or references.

void Test3()
{
    const int i = 10;
    int *p = const_cast<int*>(&i);
    *p = 20;
    cout << i << endl;
    cout << *p << endl;
}

4.dynamic_cast

Mainly used for "safe downward cast", used for pointer or reference conversion between class inheritance hierarchies. It is mainly used to perform "safe downcasting", that is, converting pointers or references of base class objects into other pointers or references at the same inheritance level.

As for "first conversion" (that is, the derived class pointer or reference type is converted to its base class type), it is safe in itself. Although dynamic_cast can be used for conversion, this is not necessary. Ordinary conversion can already achieve the purpose, after all, using dynamic_cast requires overhead.

class Base
{
public:
    Base(){};
    virtual void Show(){cout<<"This is Base calss";}
};
class Derived:public Base
{
public:
    Derived(){};
    void Show(){cout<<"This is Derived class";}
};
int main()
{
    Base *base ;
    Derived *der = new Derived;
    //base = dynamic_cast<Base*>(der); //正确,但不必要。
    base = der; //先上转换总是安全的
    base->Show();
    system("pause");
}

Recommended tutorial: "C#"

The above is the detailed content of How to convert C++ types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn