Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > some more about operator overloading

Reply
Thread Tools

some more about operator overloading

 
 
Tony Johansson
Guest
Posts: n/a
 
      05-23-2005
Hello!

I have this wrapper class Integer below that I use when testing operator
overloading.
A book that I read say that the expression
Integer i;
i+5 is translated to operator+(i,5) using the stand-alone helper function.
If Integer's constructor were not declared as explicit, the previous would
have one more possible translation which is
operator+(i, Integer(5))
for which the constructor would first convert the value 5 to the Integer
type, and then two object of type Integer would be added. Therefore the code
would have an ambiguous translation, and the compiler would refuse to
compile it.With the explicit specification, the latter possibility does not
apply, and the code compiles.

Now to my first question.Wwhy would the code have ambiguous translation?
My second question. Why does it works perfactly without this specification
explicit?
My third question in what cases would I get this mentioned about ambiguous
translation?

int main()
{
Integer i(2);
Integer k = i+5;
cout << k.get();
return 0;
}

class Integer
{
public:
Integer (int i = 0 : value_(i)
{}

int get() const
{ return value_; }

Integer operator+(const Integer& i) const
{
Integer local(value_ + i.value_);
return local;
}

friend Integer operator+(int v, const Integer& i)
{
Integer local(v + i.value_);
return local;
}

private:
int value_;
};

Integer operator+(const Integer& i, int v)
{
Integer local(v + i.get());
return local;
}

//Tony



 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-23-2005
Tony Johansson wrote:
>
> Hello!
>
> I have this wrapper class Integer below that I use when testing operator
> overloading.
> A book that I read say that the expression
> Integer i;
> i+5 is translated to operator+(i,5) using the stand-alone helper function.


right

> If Integer's constructor were not declared as explicit,


it isn't

> the previous would
> have one more possible translation which is
> operator+(i, Integer(5))
> for which the constructor would first convert the value 5 to the Integer
> type, and then two object of type Integer would be added.


right. Using the member function operator+

Therefore the code
> would have an ambiguous translation, and the compiler would refuse to
> compile it.


Wrong.
Using the standalone function no object creation is necessary, thus this
is considered to be a better match then using the member function. The
compiler would also choose this one.

> With the explicit specification, the latter possibility does not
> apply, and the code compiles.


There is no 'explicit' in the posted code.

>
> Now to my first question.Wwhy would the code have ambiguous translation?


There is no ambiguity in the posted code.

> My second question. Why does it works perfactly without this specification
> explicit?


Because for using the standalone function, no conversions are needed. Thus this
would be a perfect match. Since there is no other 'perfect match' possible, no
ambiguity arises.

> My third question in what cases would I get this mentioned about ambiguous
> translation?
>


eg.

#include <iostream>
using namespace std;

class Integer
{
public:
Integer (int i = 0 ) : value_(i)
{}

int get() const
{ return value_; }

Integer operator+(const Integer& i) const
{
Integer local(value_ + i.value_);
return local;
}

friend Integer operator+( const Integer& i, const Integer& v)
{
Integer local(v.value_ + i.value_);
return local;
}

private:
int value_;
};

int main()
{
Integer i(2);
Integer k = i+5;
cout << k.get();
return 0;
}

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments