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 1 1342
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
"constantness" of the container object. I also note that is it done
through overloading begin() and end() like below:
iterator begin();
const_iterator begin() const;
So it seems to me that I need to have two separate iterator classes
|
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 speak only of features in the spirit of C; something like
object-orientation, though a nice feature, does not belong in C.
Something like being able to #define a #define would be very handy,
though, e.g:
#define DECLARE_FOO(bar) #define...
|
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 are back (such as forms).
3. Properties can now have mixed access levels. For example you can
declare a property like this:
|
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 seems that it
discusses the std and the other part of the language with your own
abstractions. Is that better to read a book first on the basic concepts
of C++ language (but not the C part) that gives the basics as if the
reader is a beginner...
|
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?
| |
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 supported by .NET
(for example, some instances of operator overloading)!"
How can some features be supported by C# and not .NET? Are there other features
than operator overloading and could someone please supply an example?
/ Fister
|
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 & operator>(std::istream & in, const Complex & a);
// the methods correspond to the friend
std::istream & operator>(std::istream & in, const Complex & a)
{
std::cout << "real: ";
|
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,
operator =, operator, operator(), and operator-must be nonstatic
member function; this ensures that their first operands will be
lvalues". I know that these operators must be nonstatic member
functions, but why this ensure their first operands will...
|
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 overload assignment operator, such as +=, but these operator use
their simple counterpart, such as +, so you don't have to worry about that.
Overloading + means that += will function as expected.
The = operator is included in this -- it makes little...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |