overloading operator+ question

O

Ook

This is a few ways I've seen to overload operator+. I can understand that
you would want to pass a reference to the function if you wanted to change
some of the data elements of the class, but in the first example it's being
passed as a constant. Is this done for performance purposes so that it
doesn't create a copy of the class and pass the copy to the function?

Why would you have it return a reference to the class as in example 1? I
would think that the third example would be what you want to do.

Class Zoot.....

Zoot& operator+ (const Zoot& zzz); passes read only reference to class

Zoot operator+ (Zoot zzz); // passes copy of clacc

Zoot operator+ (const Zoot& zzz); // returns class, not reference - is this
right?
 
V

Victor Bazarov

Ook said:
This is a few ways I've seen to overload operator+. I can understand that
you would want to pass a reference to the function if you wanted to change
some of the data elements of the class, but in the first example it's being
passed as a constant. Is this done for performance purposes so that it
doesn't create a copy of the class and pass the copy to the function?

Why would you have it return a reference to the class as in example 1? I

*I* would not, most likely.
would think that the third example would be what you want to do.

Class Zoot.....

class Zoot { ....
Zoot& operator+ (const Zoot& zzz); passes read only reference to class

Zoot operator+ (Zoot zzz); // passes copy of clacc

Zoot operator+ (const Zoot& zzz); // returns class, not reference - is this
right?

Actually, if operator+ doesn't change _either_ operand (and it shouldn't,
if you want to follow the convention), then neither is fully correct. You
should do

Zoot operator+ (const Zoot& zzz) const;

V
 
P

persenaama

I'd want to do this, but generally my mileage varies anyway..

const zap& operator + (const zap& object);

I don't know how the cool people think about it, but for me the unary +
operator is just syntatic sugar to have symmetry with unary - operator.
$.02 and all that.

Usually I think, what kind of expression the operator would be part of
and how would the "built-in" types handle the parameters and return
value. In this case, I'm still trying to figure out why I would want to
assign to the object which might been returned if I chose to implement
this differently. Guess I'm too dumb to answer your question afterall.
That's just how I'd do it.. (oO?)
 

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

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,353
Latest member
RogerDoger

Latest Threads

Top