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 understand the C++ type conversion operator

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

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand the C++ type conversion operator", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the C++ type conversion operator.

Catalogue

Old explicit type conversion

Display type conversion of C++

Why is there a new type conversion?

Which conversion should be used?

Static_cast operator

Dynamic_cast operator

Const_cast operator

Reinterpret_cast operator

The actual conversion method of the old explicit type conversion

Summary

Old explicit type conversion

(type) c-style forced conversion of the expression

Type (expression) functional forced type conversion

1. At first, c-style type conversion was used, but in order to make the type conversion look more like a function call, functional type conversion was introduced. Functional type conversions can be converted like a function, which is better than c-style ones.

2. Generally speaking, it is recommended not to use the above two types of conversions, but to use the following four types of conversions instead. If you reject this proposal, it is recommended to use functional conversions.

Display type conversion of C++

There were many problems with the old type conversion, so a new one was introduced into C++ (of course, the so-called new one was decades ago).

Static_cast

Dynamic_cast

Const_cast

Reinterpret_cast

Why is there a new type conversion?

There are two main problems here.

First, there is no specific distinction between old-fashioned type conversions, which of the above four types, or which ones? The advantage is that it's convenient, because with old-fashioned type conversions, you don't need to tell which one you're using, just try to see if you can convert it in order. The downside is that this conversion is dangerous or may not be what the user expects. The wrong use of type conversion, but not found, is still successfully converted, this kind of behavior will bring more harm.

Second, old-fashioned type conversions are difficult to identify, whether they are c-style or functional, with only one parenthesis. Parentheses are obviously much more difficult to recognize than a form like static_cast. Therefore, in more complex expressions, if we use multiple type conversions, it is even difficult to find all the type conversions in it, which can also cause a lot of trouble.

Which conversion should be used?

In the past, when we had no choice, it was easy to use, because we didn't have to worry about which one to use. But with four options available, the problem becomes more complicated. Which one should I use? When and what should I use? Of course, if you really can't tell the difference, you should try static_cast first.

Static_cast operator

Static_cast (expression)

The most common case in static_cast is that implicit type conversions can occur between the type and the content to be converted

# include using namespace std;int main () {double a = 5 / 2; double b = static_cast (5) / 2; / / a result is 2 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