C + + Object-oriented advanced programming (vi) conversion function with non-explicit one argument ctor

Source: Internet
Author: User

Technology lies in communication, communication, reproduced please specify the source and maintain the integrity of the work.

1.conversion function Conversion Functions

    //1. Conversion Functions//conversion Function//as long as you think it's reasonable, you can write the conversion function    classFraction { Public: Fraction (intNumintDen =1): M_numerator (num), M_denominator (DEN) {}operator Double()Const //Note that there is no return type, this compiler will help and prevent us from declaring that the wrong function name is the return type        {            return((Double) m_numerator/m_denominator);//See automatic conversion rules below        }    Private:        intM_numerator; intM_denominator;        }; intMainintargcConst Char*argv[]) {Fraction F (3,5); DoubleD =4+ F;//first find the global function operator+, found no, then go find found in fraction insidecout<< D <<Endl; cout<<sizeof(Double) <<Endl; return 0; }

Automatic conversions Follow these rules:

1) If the type of the participating operands is different, it is converted to the same type first and then the operation is performed.

2) The conversion is carried out in the direction of increasing data length to ensure that the accuracy is not reduced. such as int and long operation, the int is converted into a long type before the operation.

A. If two types of bytes are different, convert to a type with a high number of bytes

B. If the two types have the same number of bytes, and one is signed, and one is unsigned, it is converted to an unsigned type

3) All floating-point operations are carried out in double precision, even if only the expression of float single-precision operation, it must first be converted to double type, and then the operation.

4) Char and short participate in the operation, you must first convert to int type.

5) In an assignment operation, the type of the amount on the right of the assignment number is converted to the type of the left quantity, not the same as the data type of the value on both sides. If the right amount of data type length is left long, a portion of the data is lost, which reduces precision and the missing part is rounded forward.

For example:

return above ((double) m_numerator/m_denominator);

If declared like this (double) (M_numerator/m_denominator) performs a division operation of type int

Therefore, to first convert the two int parameter into a double parameter can be

2.non-explicit One argument ctor

The role of explicit to prevent implicit conversions, generally acting on constructors with parameters

classfraction{ Public:    Fraction (intNumintDen =1): M_numerator (num), M_denominator (DEN) {}//Fractionoperator+(ConstFraction &f) {cout<< f.m_numerator<<Endl; returnF; }Private:    intM_numerator; intM_denominator;}; intMain () {fraction F (3,5); Fraction d= f +4;//call will make 4 implicitly converted to fraction//call process is also first to find F operator+ function found the call    return 0;}

Output results

Fraction d = f + 4; The meaning of this function is that the F call operator+ parameter is 4

and the receiving end

You will find that 4 is implicitly converted into fraction

Fraction operator+ (const fraction & F) {...}

Parameter 4 is implicitly converted to fraction

So add explicit.

classfraction{ Public:    ExplicitFraction (intNumintDen =1): M_numerator (num), M_denominator (DEN) {}//Fractionoperator+(ConstFraction &f) {cout<< f.m_numerator<<Endl; returnF; }Private:    intM_numerator; intm_denominator;};

Call End

Above I mentioned two call procedure then what happens if you use operator+ with operator double ()

classfraction{ Public:    /*Explicit*/Fraction (intNumintDen =1): M_numerator (num), M_denominator (DEN) {}//Fractionoperator+(ConstFraction &f) {cout<< f.m_numerator<<Endl; returnF; }        operator Double()Const //Note that there is no return type, this compiler will help and prevent us from declaring that the wrong function name is the return type    {        return((Double) m_numerator/m_denominator);//See automatic conversion rules below    }Private:    intM_numerator; intM_denominator;}; 

Compile will error

Reference << Houtie C + + Object-oriented advanced programming >>

C + + Object-oriented advanced programming (vi) conversion function with non-explicit one argument ctor

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.