'+' overloading

A

Aire

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

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

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

What are the differences implied by the two return types?

Thanks!
 
E

E. Robert Tisdale

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

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

Class1 Class1::eek:perator+(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.
 
D

David Harmon

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

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

Class1 Class1::eek:perator+(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.
 
J

Jumbo

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

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

Class1 Class1::eek:perator+(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::eek:perator+(const Class1& rhs){}
The this pointer will point to obj1.
HTH
 
E

E. Robert Tisdale

Jumbo said:
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::eek:perator+(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) { /* ... */ }
 
J

Jumbo

E. Robert Tisdale said:
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.
 
E

E. Robert Tisdale

Jumbo said:
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::eek:perator+(const Class1&);

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

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

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top