In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.