C++ operators, friend, member or foe?

Started by
6 comments, last by svpace 20 years, 11 months ago
Hi, very often I see implementation of binary operators(==, <, +,...) using friend functions, or plain functions sometimes, instead of implementing everything as member functions, I would like to know if there any diferences, pros, cons, things to be aware of, why do the same thing in diferent ways? class A { ... bool operator==(const A&) { ... } }; or class A {...} bool operator==(const A& a1, const A& a2) {...} any comments? [edited by - svpace on May 2, 2003 12:04:05 AM]
Advertisement
member operator might end up being faster if your compiler can pass this pointer in a register (ecx is common). if this pointer gets put on the stack, the two versions should have equivalent performance.

with that said, performance difference might be negligible, so the choice is often based on personal preferences.
stream operator overloading is impossible without friend functions.

How appropriate. You fight like a cow.
quote:Original post by Sneftel
stream operator overloading is impossible without friend functions.

Would you mind clarifying? I''m trying to learn about all this stuff

Since the LHS of the expression is the object whose operator function is called, or the global function matching both, for the expression "cout << myObj", one of these two may be called:
global operator<<(ostream, MyObject)
ostream::operator(MyObject)
Obviously, you can''t do the second, since you can''t rewrite the stream class. So you have to do the first. I misspoke earlier--stream operator overload doesn''t require friend functions, just the global operator form--but friend functions are usually used since the operator should be considered "part of the class".


How appropriate. You fight like a cow.
more info:

the version of the operator that takes you class on the LEFT SIDe can be member or global ... absolutely no difference ...

the version which takes you class on the RIGHT, but some preexisting class on the left, CANNOT BE A MEMBER FUNCTION ...


the stream classes are NOT SPECIAL CASES ... they are the same as trying to use any preexisting objects on the left ... like this:

int i(3);
OtherClass oc(4);
MyClass mc(5);

i + i; // is global in C / C++
i + oc; // must be global because int cannot be extended
oc + oc; // is supported however the writer of oc wanted
// if not already there, YOU can add as global only
i + mc; // must be global because you cannot extend builtins
oc + mc; // must be global if you cannot extend oc with a member
mc + i; // can be either, because you can extend your class mc
mc + os; // same
mc + mc; // same
Ah, so it was practically "impossible" because you''d have to rewrite the stream class. I get it. I thought you said it was impossible literally, and that was throwing me off.

I''m usually on top of these things, but the word "impossible" scares me sometimes
thank you, this question was bugging me for some time.

This topic is closed to new replies.

Advertisement