Connecting Tech Pros Worldwide Forums | Help | Site Map

overloaded operator=()

subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
#1: Dec 21 '07
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


Sachin
Guest
 
Posts: n/a
#2: Dec 21 '07

re: overloaded operator=()


On Dec 21, 10: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.
>
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
x+y = z is legal or not depends on implementation of operator+
Test Operator+(Test) // above call x+y = z works
void Operator+(Test t1, Test t2); // above call returns an error
James Kanze
Guest
 
Posts: n/a
#3: Dec 21 '07

re: overloaded operator=()


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:
x + y = z;
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
Closed Thread