How to tell the difference between member and non-member operator overloading ?
One difference between member and non-member operator overloads is that for overloads declared/defined as member functions, the number of arguments is reduced by one. For a unary operator you have no arguments, for binary operators you have one.
There are some overloads that cannot be declared/defined outside of class scope, operator=() being one of them.
In addition, a member function is scoped differently and cannot be called without an instance of the declaring type, unless it is static. Some operators, like operator=(), which must be member functions are not allowed to be static though.
First example is non-member, but it has access to class instances, and works with them ?
operator+() being a binary operator taking two arguments indicates a free function.
If the type defines a public interface that permit operator+() to work on both instances. Or if the operator is declared a friend function, granting the function access to private and protected members of the type.
In your above example, the return type is quite "interesting" as the sum of two Jazz instances is obviously supposed to be an int. You might want to question that.
Third example in also non-member, but it is of type Jazz ?
This operator is usually referred to as copy assignment operator and as already stated above, must be a non-static member function of some type T.
The standard does not mandate the return type, it could just be void, but it is convention to return a non-const reference to the assignee, i.e. the instance of type T that is being assigned to.
T& T::operator(const T& rhs)
One reason, among others, for returning a ref is that then chaining of assignment is possible:
T t;
T t2;
T t3;
t = t2 = t3; // assignment chaining
Another reason is that in some cases, the standard library expects that operator=() return a reference to the assignee for user-defined types.
For a fairly complete list of operators in C and C++, you can refer to this.
constreferences. 3. is a member, and should return by value.Jazz & operator= (const Jazz &);(in-class declaration) orJazz & Jazz::operator= (const Jazz &rhs) { /* ... */ return *this; }(out-of-class definition). More important: you can't really look at declarations without context. Member functions are declared inside a class definition, non-member functions are declared outside. You should reason on a complete example (with a class definition).