473,409 Members | 1,943 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,409 software developers and data experts.

'+' overloading

To implement the '+' overloading, the type of return value could be either
"Class1" or "Class1&".

Class1& Class1::operator+(const Class1& a, const Class1& b){ }

Class1 Class1::operator+(const Class1& a, const Class1& b){ }

What are the differences implied by the two return types?

Thanks!
Jul 22 '05 #1
6 1184
Aire wrote:
To implement the '+' overloading,
the type of return value could be either "Class1" or "Class1&".

Class1& Class1::operator+(const Class1& a, const Class1& b){ /* . . . */ }

Class1 Class1::operator+(const Class1& a, const Class1& b){ /* . . . */ }

What are the differences implied by the two return types?


The first returns a *reference* to an object of type Class1 and
the second returns and object of type 1 by value.

If you mean to implement some kind of "addition" operator,
you should implement the second.

Jul 22 '05 #2
On Sat, 24 Jan 2004 04:30:51 GMT in comp.lang.c++, "Aire"
<ai*****@zoomit.org> was alleged to have written:
To implement the '+' overloading, the type of return value could be either
"Class1" or "Class1&".

Class1& Class1::operator+(const Class1& a, const Class1& b){ }

Class1 Class1::operator+(const Class1& a, const Class1& b){ }

What are the differences implied by the two return types?


Typically, returning a reference from operator+ is a mistake.

In returning a value, the implication is that the value is copied to a
result location arranged for it. This copy may actually be eliminated
if the compiler is able to do so (called the return value optimization.)

In returning a reference, you the programmer must arrange for memory to
store the value, Automatic storage in the called routine cannot be used
for this, since it is deallocated when the function returns before the
value can be used. Dynamic memory creates the problem of when and where
to release it, and static memory causes problems for reentrant routines
and requires saving the value before calling the function again.
Jul 22 '05 #3

"Aire" <ai*****@zoomit.org> wrote in message
news:%x*********************@bgtnsc05-news.ops.worldnet.att.net...
To implement the '+' overloading, the type of return value could be either
"Class1" or "Class1&".

Class1& Class1::operator+(const Class1& a, const Class1& b){ }

Class1 Class1::operator+(const Class1& a, const Class1& b){ }

What are the differences implied by the two return types?

Thanks!

You should return a copy of a Class1 object therefore no reference is
required.
Also you only pass 1 parameter e.g:
obj3 = obj1+ obj2;
In the above situation the + operator would be called on obj1 so the
parameter would be obj2. The return type would be a value which is to be
assigned to obj3.

So you need Class1 Class1::operator+(const Class1& rhs){}
The this pointer will point to obj1.
HTH

Jul 22 '05 #4
Jumbo wrote:
Aire wrote:
To implement the '+' overloading,
the type of return value could be either "Class1" or "Class1&".

Class1& Class1::operator+(const Class1& a, const Class1& b){ }

Class1 Class1::operator+(const Class1& a, const Class1& b){ }

What are the differences implied by the two return types?

Thanks!


You should return a copy of a Class1 object therefore no reference is
required.
Also you only pass 1 parameter e.g:
obj3 = obj1+ obj2;
In the above situation the + operator would be called on obj1 so the
parameter would be obj2. The return type would be a value which is to be
assigned to obj3.

So you need Class1 Class1::operator+(const Class1& rhs){}
The this pointer will point to obj1.


Of course. I completely missed that. Aire probably meant to write:
Class1 operator+(const Class1& a, const Class1& b) { /* ... */ }

Jul 22 '05 #5

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Jumbo wrote:
Aire wrote:
To implement the '+' overloading,
the type of return value could be either "Class1" or "Class1&".

Class1& Class1::operator+(const Class1& a, const Class1& b){ }

Class1 Class1::operator+(const Class1& a, const Class1& b){ }

What are the differences implied by the two return types?

Thanks!


You should return a copy of a Class1 object therefore no reference is
required.
Also you only pass 1 parameter e.g:
obj3 = obj1+ obj2;
In the above situation the + operator would be called on obj1 so the
parameter would be obj2. The return type would be a value which is to be
assigned to obj3.

So you need Class1 Class1::operator+(const Class1& rhs){}
The this pointer will point to obj1.


Of course. I completely missed that. Aire probably meant to write:
Class1 operator+(const Class1& a, const Class1& b) { /* ... */ }

I'm not with you, that is what she/he wrote is it not? Except that his/hers
was out of class definition.
Jul 22 '05 #6
Jumbo wrote:
E. Robert Tisdale wrote:
Of course. I completely missed that. Aire probably meant to write:

Class1 operator+(const Class1& a, const Class1& b) { /* ... */ }
I'm not with you. That is what she/he wrote is it not?


It is not.
Except that [Aire's] was out of class definition.


Aire attempted to defined member

Class1::operator+(const Class1&);

with *two* arguments which is a mistake.
I'm saying that Aire probably meant to define a nonmember operator+.

Jul 22 '05 #7

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

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
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...
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.