Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert the type of C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how C++ carries on the type conversion, the content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe you will gain something after reading this C++ article on how to carry out type conversion. let's take a look.

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

The operating environment of this tutorial: windows7 system, Craft 17 version, Dell G3 computer.

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

They are essentially templates.

Here are the following:

1.static_cast

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

This conversion is done at compile time, similar to C-style type conversion, but note the following

Cannot convert between two class types that have no derived relationship

Type modifiers of existing types, such as const,volatile,__unaligned, cannot be removed.

Because there is no dynamic type checking when converting objects, there is a security risk when converting from base class objects to derived class objects.

The way in void Test () {/ / C is int I = 10; double D1 = iTracer / implicit type conversion / / int* p = iX / implicit type conversion can only be forced type conversion int* p = (int*) iBank / C++ way double D2 = static_cast (I) / / it is equivalent to creating an anonymous object of type static_cast and assigning a value to d2 int* p2 = static_cast (I); / / if it cannot be converted, an error will be reported}

2.reinterpret_cast

The meaning of reinterpret is to reinterpret and convert one type to another unrelated type, corresponding to the forced type conversion in C, dealing with situations where implicit conversion cannot be performed.

Void Test () {int I = 10; int* p2 = reinterpret_cast (I);}

Forced type conversion can sometimes deal with problems violently.

Such as the following example:

For a function with parameters, how can it be called without passing parameters?

Void Fun (int s) {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report