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