Why is that sometimes an operator override is defined as a method in the class, like
MyClass& MyClass::operatorFoo(MyClass& other) { .... return this; };
and sometimes it’s a separate function, like
MyClass& operatorFoo(MyClass& first, MyClass& bar)
Are they equivalent? What rules govern when you do it one way and when you do it the other?
If you want to be able to do something like
3 + objyou have to define a free (non-member) operator.If you want to make your operators protected or private, you have to make them methods.
Some operators cannot be free functions, e.g.,
operator->.This is already answered here:
difference between global operator and member operator