Implicit Conversions


[ Follow Ups ] [ Post Followup ] [ CS2704 Web Discussion Board ] [ FAQ ]

Posted by Michael Young on May 03, 2001 at 23:26:51:

From the notes and class lectures, I know that if there is a constructor that takes one parameter, then that is used to convert from that type.

So, if class Rational contained:

Rational(int Top, int Bottom=1);
Rational operator+(const Rational& RHS);

Then you could write:

Rational A, B(1);
A=B+3;

and it would compile. 3 would be coverted to a Rational and added to B. However, what if you were to write:

A=3+B;

Would the compiler know to convert 3 to a Rational? If not, how can we make it, aside from explicitly saying it? 'cause, it seems natural to write it that way.

TIA!
Michael Young


Follow Ups:



Post a Followup

Name:
E-Mail:

Subject:

Comments:

Optional Link URL:
Link Title:
Optional Image URL:


[ Follow Ups ] [ Post Followup ] [ CS2704 Web Discussion Board ] [ FAQ ]