Hey guys, would this be correct(practising old exams)
example when an operator overloaded function could be member or global function
++, --,!=
member function only
(), ->, []
global function only
<<, >>
Hey guys, would this be correct(practising old exams)
example when an operator overloaded function could be member or global function
++, --,!=
member function only
(), ->, []
global function only
<<, >>
Hmm,No.Originally posted by noob2c
global function only
<<, >>
Code:class Foo { int val; public: ostream& operator<<(ostream& os) { return os << val; } Foo& operator >>(int i) { val >>= i; return *this; } }; int main() { Foo foo; foo << cout; Foo bar = foo >> 3; }
then which would fall under that category?
I always remember being told they were either members functions or friend functions. Members if they modified the class data (e.g +=) and friends if they dont (e.g. <<).
Couldn't think of anything interesting, cool or funny - sorry.
They don't NEED to be friends; they can be global functions that have no privileged access if they don't NEED that privileged access.
operators are part of the interface of the class, but there is no rules about which ones can/should be members or friends or globals. They should exist in the same namespace as the class, but that's about it.