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 achieve type conversion in C++

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how C++ to achieve type conversion, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

C++ has implicit or explicit type conversions for built-in types, such as int,double,long,char. However, C++ also has the concept of class, which is a custom type. In order to make the class (custom type) reach the level of built-in type, C++ also provides some methods for automatic or forced type conversion.

A lot of the work of C++, in my opinion, is to make custom types as easy to use and operate as built-in types.

Conversions of built-in types int a = 8; double b = 4.4; long c = a _

This is what we are familiar with, and the compiler has taken care of it for us.

Custom type conversion

For custom types, we can implement and control the type conversion by ourselves.

/ * * declarative definition of stone class * / class Stone {public: Stone (double weight); Stone (int stone_type, double volume); private: int _ stone_type; double _ weight; double _ volume;}; Stone::Stone (double weight) {_ stone_type = 1; _ weight = weight; _ volume = 10.5 } Stone::Stone (int stone_type, double volume=10.5) {_ stone_type = stone_type; _ weight = 5.8; _ volume= volume;}

We provide an overload of the constructor Stone (double), which can directly construct a class from the double type.

Stone S1 = 24.5; Stone S2 (10.5); Stone S3 (21,20.5)

For Stone S1 = 24.5;, the constructor Stone (double) again creates a temporary Stone object with 24.5 as the initial value, and then copies the contents of the temporary object to the S1 object using the member-by-member complex value method. That is, an object of type double is converted to an object of type Stone.

This process, called implicit conversion, is automatic and does not require explicit migration to type conversion.

Note: only constructors that accept one parameter can be used as conversion functions.

For example, Stone (int stone_type, double volume) has two parameters, so it cannot be used to convert types. However, if its second-generation parameter is a default and provides a default value, it can be used to convert int objects.

This conversion function converts those other (built-in or other types) types to class types

Explicit keyword

Using the constructor as an automatic type conversion function seems like a good feature, but this automatic conversion is not required in all cases, in some cases, it is not needed, but the conversion is unexpected.

So C++ provides the keyword explicit, which is used to turn off this automatic conversion.

Explicit means: explicit, explicit.

Can be added before the function declaration

Explicit Stone (double weight)

In this way, only this construct can be explicitly called.

Stone S1 = Stone (24.5); Stone S4 = (Stone) 19.99

There is no problem with explicit invocation.

A reminder

One more reminder: if it's like this,

A function with two arguments

One added explicit, the other did not add, if it is still the same as just now, implicit conversion of the kind, can still be soaked, because, will execute the constructor of the two parameters, because this is the match, which will cause a hidden danger, to remind you to add explicit, it is best to add all the overloaded functions, otherwise it will be difficult to find Bug.

Question: when does the compiler use Stone (double)?

If the keyword explicit is used in the declaration, Stone (double) can only be used to explicitly cast

If not, it can also be used for implicit type conversion.

When initializing a Stone object to a Stone value, for example: S1 = 24.5

The double value is assigned to the Stone object, for example: Stone S5 TX S5 = 19.7

Pass the double value to the function that accepts the Stone parameter, for example:

Void Print (const Stone& tmp) {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