473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

some more about operator overloading

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

Jul 23 '05 #1
1 1322
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
kb******@gascad.at
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
19
by: Chris Dunaway | last post by:
Just got my hands on Whidbey. Here are a few interesting changes that you may not be aware of: 1. Continue statement on Do, For and While commands 2. Default Instances of certain classes...
36
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
7
by: Fister | last post by:
I'm reading Professional C# (Wrox) and stumbled across: "Some features are supported by.NET but not by C#, and you might be surprised to learn that some features of the C# language are not...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
8
by: Tony Johansson | last post by:
Hello! I think that this text is complete enough so you can give me an explanation what they mean with it. I wonder if somebody understand what this means. It says the following : "You can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.