Overloaded operators ---- members or friends

M

masood.iqbal

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
 
O

osmium

<partial answer only>

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

Generic Usenet Account

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
 
N

Noah Roberts

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top