Connecting Tech Pros Worldwide Help | Site Map

overloading operator+ question

Ook
Guest
 
Posts: n/a
#1: Oct 11 '05
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?


persenaama
Guest
 
Posts: n/a
#2: Oct 11 '05

re: overloading operator+ question


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?)

Victor Bazarov
Guest
 
Posts: n/a
#3: Oct 11 '05

re: overloading operator+ question


Ook wrote:[color=blue]
> 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[/color]

*I* would not, most likely.
[color=blue]
> would think that the third example would be what you want to do.
>
> Class Zoot.....[/color]

class Zoot { ....
[color=blue]
> 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?[/color]

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
Closed Thread