Question on implicit type conversion


R

Ruby Stevenson

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
 
Ad

Advertisements

A

Alf P. Steinbach

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
 
Ad

Advertisements

R

Richard Damon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top