Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

svpace

C++ operators, friend, member or foe?

This topic is 5704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
stream operator overloading is impossible without friend functions.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!