On Dec 21, 6:20 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Quote:
overloaded operator=()
--------------------------------
overloaded assignment operator should be a non-static MEMBER function
of a class. This ensures that the first operand is an lvalue.
No it doesn't.
The reason a user defined assignment operator (overloaded or
not) should be a member is first and foremost because the
standard doesn't allow it to be a non-member. The reason the
standard doesn't allow this is because if there isn't a user
declared copy assignment operator, the standard implicitly
declares one.
Quote:
If the overloaded assignment operator function is allowed to
be a non-member function then we may be able to write the
following:
Quote:
Suppose we have a class Test for which operator+() is defined,
suppose we have
Quote:
Test operator=(Test lhs, Test rhs)
{
Test obj;
//...
Return obj;
}
I suppose that the above is a typo; that you meant to define
operator+. (Defining an operator= which took all of its
parameters by value wouldn't make any sense.)
Quote:
Test x;
Test y;
Test x;
Quote:
This is wrong because x + y is not an lvalue but would become
legal due to the above definition of overloaded operator=().
That is why operator=() should be a member function. Is this
understanding of mine is correct ?
Not at all. In fact, if operator+ returns a non-const object
(which is usually the case), then the above is legal.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34