473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overloaded operators ---- members or friends

Please help me with this doubt that I have regarding overloaded
operators. Sometimes they are member functions and sometimes they are
friends (e.g. see the code snippet from Stroustrup, Second Edition that
I have posted to comp.sources.d). How do we decide which is more
appropriate? Why are the overloaded "<<" and ">>" operators always
friends?

Also, what is an appropriate application for the overloaded function
call operator?

Masood

Jul 23 '05 #1
4 1587
<partial answer only>

<ma**********@lycos.com> wrote:
Please help me with this doubt that I have regarding overloaded
operators. Sometimes they are member functions and sometimes they are
friends (e.g. see the code snippet from Stroustrup, Second Edition that
I have posted to comp.sources.d). How do we decide which is more
appropriate?


Use a friend when you want symmetry. Note that sometimes a = b + c is not
the same as a = c + b. Depending on what type b and c are.

As a rule of thumb, I think that friends should be avoided unless there is
an actual payoff as here.
Jul 23 '05 #2
I asked a question simular to this yesterday, if you are interested you
can see here

http://groups-beta.google.com/group/...30da0322ef8477

Jul 23 '05 #3
ma**********@lycos.com wrote:
Please help me with this doubt that I have regarding overloaded
operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second Edition that I have posted to comp.sources.d). How do we decide which is more
appropriate? Why are the overloaded "<<" and ">>" operators always
friends?

Also, what is an appropriate application for the overloaded function
call operator?

Masood


The rule of thumb is that if the left operand of the operation should
be the object in question, make the overloaded operator a member
function, otherwise make it a friend. Actually there are just a
handful of operators for which it makes sense to declare the overloaded
operator as a friend.

Hope that helps!

Gus

Jul 23 '05 #4

ma**********@lycos.com wrote:
Why are the overloaded "<<" and ">>" operators always
friends?


Because with member functions the left hand operand is the object. But
with << and >> the left hand operand is usually the stream.

You could create a >> member function that printed to out...
class Object {
....
XXX operator>>(&ostream);
....
};

then you would do:

object >> cout;

But what does that function return? Probably not an ostream, so you
can't really line up output calls that way...actually it isn't really
that useful. I would have to murder anyone that did that to anything I
had to work with...

Also, operator << and >> are not always friends. Some would say it is
bad design if they are. They can be regular functions that call
accessor methods on the object in question.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.