Most operators should be defined as members.
class MyClass
{
...
public:
const MyClass& operator+=(const MyClass&);
};
Bit this is identical in behavior to the following:
class MyClass {...};
const MyClass& operator+=(const MyClass&, const MyClass&);
The implied this
in the first example is analagous to the first parameter to the second example. If the second example needs access to the internal state of MyClass
, it needs to be friend
ed.
class MyClass
{
friend const MyClass& operator+=(const MyClass&, const MyClass&);
};
const MyClass& operator+=(const MyClass&, const MyClass&);
The prototypical exception to this is operator<<
on std::ostream
.
std::ostream& operator<<(std::ostream&, const MyClass&);
This is logically a member of your MyClass
, but because of the ordering of the parameters it would have to be a non-member of both classes or a member of std::ostream
. Because you can't add members to std::ostream
, this must be defined as a non-member.