Get the App
SLTechnology News&Howtos  ›  Development  › 

The conversion method between the functions of C++

Shulou Source: shulou.com Published: 2022-06-03 15:30:03 10月03日 Update

This article mainly explains "the conversion method between the functions of C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the conversion method between the functions of C++".

1. Implicit type-safe conversions are performed between standard data, as follows:

This paper mainly discusses the conversion between class types and normal types in C++:

1. Class types convert normal types

Class Fraction {public: Fraction (int num,int den=1); ~ Fraction (); / / conversion function / * conversion function syntax rules: operator Type () {Type ret; .return ret } * / operator double () const {return (double) (m_Numerator*1.0 / m_Denominator);} private: int masked Numerator; int masked Denominator;}; Fraction f (3,5); double dong4 + fanspace

Here 4roomf, call the conversion function to convert f to 0.6

two。 Convert normal types to class types (non-explicit-one-argument ctor)

Class Fraction {public: Fraction (int num,int den=1); ~ Fraction (); Fraction operator+ (const Fraction& f) {return Fraction (this- > m_Numerator * f.m_Denominator + this- > m_Denominator * f.m_Numerator, f.m_Denominator * this- > m_Denominator);} private: int masked Numerator; int masked Denominator;} Fraction f (3,5); Fraction d = f + 4 position bank / call + operator function, 4 will be converted to Fraction (4jue 1), with a constructor that accepts one parameter, otherwise it cannot be converted.

3. Both the conversion function and the implicit call to the constructor (conversion constructor parameters) will report an error.

Class Fraction {public: / * conversion constructor:-there is only one parameter-the parameter is a basic type-the parameter is of another type * / Fraction (int num,int den=1); ~ Fraction (); operator double () const {return (double) (m_Numerator*1.0 / m_Denominator) } Fraction operator+ (const Fraction& f) {return Fraction (this- > m_Numerator * f.m_Denominator + this- > m_Denominator * f.m_Numerator, f.m_Denominator * this- > m_Denominator);} private: int masked Numerator; int masked Denominator;}

Error: "Fraction::operator +": 2 overloaded with similar conversions

So when we write constructors, we use the keyword explicit to prevent implicit type conversions.

Class Fraction {public: explicit Fraction (int num,int den=1); ~ Fraction (); operator double () const {return (double) (m_Numerator*1.0 / m_Denominator) } Fraction operator+ (const Fraction& f) {return Fraction (this- > m_Numerator * f.m_Denominator + this- > m_Denominator * f.m_Numerator, f.m_Denominator * this- > m_Denominator);} private: int masked Numerator; int masked Denominator;}; Fraction f (3,5); Fraction d = f + 4 / / error report: there is no appropriate constructor from "double" to "Fraction" double d = f + 4 role / can be, f can be converted to this, I believe you have a deeper understanding of the "conversion method between C++ functions", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Functions types parameters between methods Clearing common content rules learning practical deeper similar appropriate security keys keywords interest at the same time practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info Microsoft Shulou Technology Apple