By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

Overloaded operators ---- members or friends

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
<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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.