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 use the conversion function of a class in C++

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

Share

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

Today, the editor will share with you the relevant knowledge points about how to use the conversion function of the class in C++. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

Only a constructor that accepts one parameter (other parameters have default values) can be used as a transformation constructor.

In C++, a constructor that takes a parameter provides a blueprint for converting a value of the same type as that parameter into a class. Therefore, the following constructor is used to convert the value of type double to type Stonewt:

Stonewt (double lbs) / / double to Stonewt template

That is, you can write code like this:

Stonewt myCat; / / create a Stonewt object myCat = 19.6; / / convert 19.6 to a Stonewt object using Stonewt (double)

This process is called implicit conversion because it is automatic and does not require explicit casting.

Using the constructor as an automatic type conversion seems like a good feature. However, when programmers have more experience with C++, they will find that this automatic feature is not always desirable because it can lead to unexpected type conversions. As a result, C++ has added the keyword explicit to turn off this automatic feature. That is, you can declare the constructor as follows:

Explicit Stonewt (double lbs) / / implicit conversion is not allowed

This turns off the implicit conversion described in the above example, but still allows explicit conversion, that is, explicit casting:

Stonewt myCat;myCat = 19.6; / / not allowed, because Stonewt (double) is declared as explicitmyCat = Stonewt (19.6); / / OK, a display conversion myCat = (Stonewt) 19.6; / / OK, the original display type conversion

When will the compiler use the Stonewt (double) function?

If the keyword explicit is used in the declaration, Stonewt (double) will only be used for explicit casting, otherwise it can also be used for the following implicit conversions:

When the Stonewt object is initialized to a serial value. For example, Stonewt st.

When assigning a double value to a Stonewt object. For example, Stonewt st; st = 1.23

When the double value is passed to the function that accepts the Stonewt parameter. For example, display (Stonewt& st);-> display (1.23)

When the return value is declared as Stonewt when the function attempts to return the string value. Such as: Stonewt& go () {return 1.23;}

In either case, when using a built-in type that can be converted to a double type. Such as: Stonewt& go () {return 123;}

You can convert numbers to Stonewt objects. Can I do the opposite conversion? That is, is it possible to convert the Stonewt object to a serial value, as shown below?

Stonewt wolfe (285.7); double host = wolfe;//? Is that okay?

You can do this, but not using a constructor. The constructor is only used for conversion from a certain type to a class type. To do the reverse conversion, a special C++ operator function, the conversion function, must be used.

Conversion functions are user-defined casts that can be used as if they were cast. For example, if you define a conversion function from Stonewt to double, you can use the following transformation:

Stonewt wolfe (285.7); double host = double (wolfe); / / syntax # 1double thinker = (double) wolfe; / / syntax # 2 can also let the compiler decide what to do: Stonewt wells (20,3); double star = wells; / / implicitly use the conversion function

The compiler finds that there is a Stonewt type on the right and a double type on the left, so it will see if the programmer has defined a conversion function that matches this. (if such a definition is not found, the compiler generates an error message indicating that Stonewt cannot be assigned to double. )

To create a conversion function that is converted to a typeName type, you need to use this form of conversion function:

Operator typeName (); / / No return type, no parameters

Please note the following points:

The conversion function must be a class method

The conversion function cannot specify a return type

Conversion function cannot have parameters

Define in the header file of the class:

Operator int () const;operator double () const

Implementation code:

. / / construct Stonewt object from stone, double valuesStonewt::Stonewt (int stn, double lbs) {stone = stn; pds_left = lbs; pounds = stn * Lbs_per_stn + lbs;}. / / conversion functionsStonewt::operator int () const {return int (pounds + 0.5);} Stonewt::operator double () const {return pounds;}

Use:

Stonewt poppins (9,2.8); / / 9 stone, 2.8poundsdouble p_wt = poppins;// implicit conversioncout

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