Operator Overloading Problem

Hi
Why sometimes we ovrload operator as member function and sometimes as friend function ( and out of class body ) ?
When we overload an operator as a member function then our class is always the left hand side of the expression. If we want a different class than our own to be the left hand side then we need to overload an external function.

For example if I want to add MyType objects to integers, I need to overload an external function to put integers on the left hand side of the expression: myType1 = 4 + myType2; But for adding two objects of MyType it can be a member function: myType1 = myType2 + myType3;
Because some operators must be overloaded as member function. If I remember correctly, those are [], =, -> and (). Galik gave good example.
Last edited on
Topic archived. No new replies allowed.