Reputation: 3455
In these c++ examples:
1. int operator+ ( Jazz &lhs,Jazz &rhs );
2. Jazz & Jazz operator= (const Jazz &);
3. Jazz & operator+ ( const Jazz & )
I understand the basic concepts behind operator overloading in c++. What I don't know is:
In the second example, do we need to declare and define copy constructor function before overloading operator= ?
How to tell the difference between member and non-member operator overloading ?
First example is non-member, but it has access to class instances, and works with them ?
Third example in also non-member, but it is of type Jazz ?
Can someone explain please. Thank you.
Upvotes: 0
Views: 559
Reputation: 2904
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.
Upvotes: 0
Reputation: 87959
Example one must be a non-member operator. Because operator+
has one or two arguments if a non-member but zero or one if a member. It has no special access to Jazz
, it's the same as any other function in that respect. If you want to give it special access to Jazz
you would declare it a friend
(again just like any other function).
Example two must be a member, that's just a rule of C++, operator=
must be a member function. You don't have to declare a copy constructor as well. It's just that's it's very common that if you need a copy constructor you will also need an assignment operator and vice versa.
Example three could be a member or non-member (see answer 1). If it was a member it would define a binary operator (i.e. for uses like a + b
), but if it were a non-member it would define a unary operator (i.e. for uses like +a
). Either way I don't think it's right to say that it's 'of type Jazz'. It could be a member of class Jazz, but it doesn't have to be, it could be a member of a different class entirely.
Upvotes: 2