Footprint C ++ primer 49. overload, conversion, operator, primer Operator

Source: Internet
Author: User

Footprint C ++ primer 49. overload, conversion, operator, primer Operator
Overload, Conversion, and Conversion Operators
Conversion Operator
Operator type () const
Conversions
An array or a function type are not permitted.
The conversion function must be a member function, and the return type cannot be specified. There must be an empty parameter list.
The function should normally be const.

Defining a Class with a Conversion Operator (for English-loving friends) defines a Class, indicating an integer in the range of 0 to 255.

Class SmallInt {public: SmallInt (int I = 0): val (I) {if (I <0 | I> 255) throw std: out_of_range ("out-of-bounds !!! Invalid cross-border goal score! ") ;}Explicit operator int () const {return val;} // conversion operator, which cannot have parameters. It is a member function and does not return the private: size_t val;} type ;};

Implement the following:
Void fun1 () {SmallInt si; si = 4; // bar 4 is implicitly converted to the SmallInt type // si + 3; // si is implicitly converted to the int type, because an error occurs in explicit, only explicit conversions can be performed after the explicit is added}

Conversion Operators Can Yield Suprising Results
Overload operators can produce amazing results
void fun2(){    int i=42;//    cin<<i;   this code would be legal if the conversion to bool were not explicit!}

That's what it means, but I don't feel it!
Explicit Conversion Operators
Clear conversion operation
To put it bluntly, let's use explicit!

Full code! This is concise. Haha!
/*** Function: heavy load, conversion, operator * Time: July 19, 2014 15:05:21 * Author: cutter_point */# include <iostream> # include <stdexcept> using namespace std; /************************************ Conversion operators conversion operator ************************************* * // * operator type () constConversions toan array or a function type are not permitted. the conversion function must be a member function, and the return type cannot be specified. There must be an empty parameter list. The function should normally be const. * // ** Defining a Class with a Conversion Operator * // defines a class, which indicates an integer Class SmallInt {public: smallInt (int I = 0): val (I) {if (I <0 | I> 255) throw std: out_of_range ("out of bounds !!! Invalid cross-border goal score! ") ;}Explicit operator int () const {return val;} // conversion operator, which cannot have parameters. It is a member function and does not return the private: size_t val;} type ;}; void fun1 () {SmallInt si; si = 4; // bar 4 is implicitly converted to the SmallInt type // si + 3; // si is implicitly converted to the int type, because an error occurs in explicit, only explicit conversions Can be performed after the explicit is added.}/** Conversion Operators Can Yield Suprising Results overload Operators Can generate amazing Results */void fun2 () {int I = 42; // cin <I; this code wocould be legal if the conversion to bool were not explicit !} /** Explicit Conversion Operators clearly defines the Conversion operation * // to put it bluntly, we will use explicit! Int main () {return 0 ;}

For me, there will certainly be many misfortunes and pains when people live in this world, and such unfortunate events have never come one by one, they will all come when you are caught off guard, just like a savvy Hunter, pouring all your energy into your body as soon as you reveal flaws, ensuring you are safe. It's impossible for everyone to make mistakes all their lives, but when bad luck comes, we should always be ready, don't be panic, calm down, think about how you should face it!






Which of the following functions cannot overload the operator? (B)

B. The overload operator is actually a function. It can be a member function of a class, a common function [global function], or a member function, which is the thing you need to overload the operator.

C language heavy-duty operator

1. C language is not object-oriented, there is no class keyword, and there is no operator overload. I guess you are using the vc programming environment. 2. A & indicates A reference. If no reference is required, the argument of A real parameter is displayed. modifying A parameter does not affect the value of the real parameter. If A reference is used, real parameters and form parameters refer to the same variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.