S
subramanian100in
overloaded operator=()
--------------------------------
overloaded assignment operator should be a non-static MEMBER function
of a class. This ensures that the first operand is an lvalue.
If the overloaded assignment operator function is allowed to be a non-
member function then we may be able to write the following:
Suppose we have a class Test for which operator+() is defined,
suppose we have
Test operator=(Test lhs, Test rhs)
{
Test obj;
//...
Return obj;
}
Test x;
Test y;
Test x;
x + y = z;
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 ?
Kindly clarify.
Thanks
V.Subramanian
--------------------------------
overloaded assignment operator should be a non-static MEMBER function
of a class. This ensures that the first operand is an lvalue.
If the overloaded assignment operator function is allowed to be a non-
member function then we may be able to write the following:
Suppose we have a class Test for which operator+() is defined,
suppose we have
Test operator=(Test lhs, Test rhs)
{
Test obj;
//...
Return obj;
}
Test x;
Test y;
Test x;
x + y = z;
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 ?
Kindly clarify.
Thanks
V.Subramanian