Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-07-2012, 06:06 PM
Ruby Stevenson
Guest
 
Posts: n/a
Default Question on implicit type conversion

All -

I am reading Effective C++, item 24 on implicit type conversion. The
example given is that:

class Rational {
public:
Rational (int numerator=0, int denominator=1);
const Rational operator*(const Rational &rhs) const;
}

Rational oneHalf(1,2);

result = oneHalf * 2; // fine
result = 2 * oneHalf; //error

The book then says "It turns out that parameters are eligible for
implicit type conversion only if they are listed in the parameter
list" ...

I am confused as to what parameter list it is talking about??

thanks

Ruby
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 03-07-2012, 06:32 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Question on implicit type conversion

On 07.03.2012 20:06, Ruby Stevenson wrote:
>
> I am reading Effective C++, item 24 on implicit type conversion. The
> example given is that:
>
> class Rational {
> public:
> Rational (int numerator=0, int denominator=1);
> const Rational operator*(const Rational&rhs) const;
> }
>
> Rational oneHalf(1,2);
>
> result = oneHalf * 2; // fine
> result = 2 * oneHalf; //error
>
> The book then says "It turns out that parameters are eligible for
> implicit type conversion only if they are listed in the parameter
> list" ...
>
> I am confused as to what parameter list it is talking about??


In the first case '2' is implicitly converted to 'Rational', because the
left hand side operand of '*' is of type 'Rational' and that type has a
member function 'operator*' that requires the right hand side operand to
be of type 'Rational const&'.

In the second case the left hand side operand is '2', of type 'int'. The
only '*' operator associated with that type is the built-in '*'. The
built-in arithmetic operators like '*' do force some implicit
conversions, called "promotions", essentially bringing both operands up
to the same common supertype, but type `Rational` does not offer any
conversion to any of the built-in arithmetic types.

What if it did, though?


<code>
#include <iostream>

struct Rational
{
operator double() const { return 7; }
};

int main()
{
using namespace std;

cout << 6*Rational() << endl;
}
</code>


That works perfectly fine (according to the C++ standard, not just
according to a specific compiler), so the book is a bit misleading.


Cheers & hth.,

- Alf
Reply With Quote
  #3 (permalink)  
Old 03-07-2012, 07:37 PM
Richard Damon
Guest
 
Posts: n/a
Default Re: Question on implicit type conversion

On 3/7/12 2:06 PM, Ruby Stevenson wrote:
> All -
>
> I am reading Effective C++, item 24 on implicit type conversion. The
> example given is that:
>
> class Rational {
> public:
> Rational (int numerator=0, int denominator=1);
> const Rational operator*(const Rational &rhs) const;
> }
>
> Rational oneHalf(1,2);
>
> result = oneHalf * 2; // fine
> result = 2 * oneHalf; //error
>
> The book then says "It turns out that parameters are eligible for
> implicit type conversion only if they are listed in the parameter
> list" ...
>
> I am confused as to what parameter list it is talking about??
>
> thanks
>
> Ruby


When trying to find the match to the second operation of
(int) * (Rational)
The compiler does not look to convert the first parameter to find class
member functions. (This is what I think the books comment refers to,
that the implicit Rational* this parameter is not allowed to use
implicit type conversion).

On the other hand, if your operator* was defined not as a member
function, but as a free function with the signature:

Rational operator*(const Rational& lhs, const Rational& rhs);

then promotion of either lhs or rhs can occur. For this reason, it is
often better to make binary operators non-member functions to maintain
this symmetry.

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:42 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.