Connecting Tech Pros Worldwide Help | Site Map

overloading operator+ question

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 11th, 2005, 05:55 PM
Ook
Guest
 
Posts: n/a
Default overloading operator+ question

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?



  #2  
Old October 11th, 2005, 06:15 PM
persenaama
Guest
 
Posts: n/a
Default 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?)

  #3  
Old October 11th, 2005, 06:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.