"Footstep C++primer" 49, overloading, change, operator

Source: Internet
Author: User

overload, change. OperatorsConversion Operators
Conversion operators
operator type () const
Conversions to
An array or a function type is not permitted.
The conversion function must be a member function. You cannot specify a return type, you must have an empty list of references.
Functions should usually be const.

defining a Class with a Conversion Operator (dedicated to English-loving friends)defines a class that represents an integer in a range of 0 to 255 classes
Class Smallint{public:    SmallInt (int i=0): Val (i)    {        if (i<0 | | i>255)            throw Std::out_of_range (" Crossed!

!!

Cross-border goals are invalid!

"); } explicit operator int () const {return val;} The conversion operator. Cannot have a number of parameters, is a member function, no return type private: size_t Val;};


Implement the following:
void Fun1 () {    SmallInt si;    si=4;   4 implicitly converted to smallint type//    si+3;   The implicit conversion si is of type int, because explicit will go wrong here, adding explicit can only be explicitly converted}

Conversion Operators Can Yield suprising Results
Overloaded operators can produce amazing results.
void Fun2 () {    int i=42;//    cin<<i;   This code would is legal if the conversion to bool were not explicit!}

Say so but what is the amazing effect, anyway I do not feel!


Explicit Conversion Operators
Understanding Conversion Operations
It is said that we use explicit to use!



Full code! This time is concise point haha!

/*** function: overloaded. 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 is not permitted. The conversion function must be a member function. You cannot specify a return type, you must have an empty list of references. Functions should usually be const. *//**defining a class with a Conversion operator*///defines a category. Represents an integer class smallint{public:smallint (int i=0) in a range of 0 to 255 classes: Val (i) {if (i<0 | | i>255) throw s Td::out_of_range ("Out of bounds!!! Cross-border goals are invalid!    ");  } explicit operator int () const {return val;} The conversion operator. Cannot have a number of references. is a member function. No return type private:size_t val;};    void Fun1 () {SmallInt si;   si=4;   4 implicitly converted to smallint type//si+3; The implicit conversion si is of type int, because explicit will be faulted here. After adding explicit, you can only explicitly convert}/**conversion Operators can yield suprising results overloaded operators can produce amazing results */void fun2 () {int i=42;//cin&l   t;<i; This code would is legal if the conversion to bool were not explicit!} /**explicit CoNversion operators understand the conversion operation *///plainly is that we use explicit to use! int main () {return 0;}

I would say that people live in this world will certainly encounter a lot of misfortune and pain, and generally such unfortunate things are never one, they will be unprepared when you come all of a sudden. Just like a shrewd hunter. Put all your energy on your body in a moment of weakness and make sure it's foolproof. Every man can not make mistakes in his life, but when bad luck comes, we must always be ready, don't panic. Calm down and think about how you should face it!





Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

"Footstep C++primer" 49, overloading, change, operator

Related Article

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.