overloaded operator=()



 DEVELOP > c-Plus-Plus > overloaded operator=()

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "India"
Date: 20 Dec 2007 11:20:12 PM
Object: overloaded operator=()
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
.

User: "Sachin"

Title: Re: overloaded operator=() 21 Dec 2007 12:02:43 AM
On Dec 21, 10:20=A0am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:

overloaded operator=3D()
--------------------------------
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 =A0we have a class Test for which operator+() is defined,
suppose we have

Test operator=3D(Test lhs, Test rhs)
{
=A0 =A0 =A0 =A0 Test obj;
//...
Return obj;

}

Test x;
Test y;
Test x;

x + y =3D z;

This is wrong because x + y is not an lvalue but would become legal
due to the above definition of overloaded operator=3D(). That is why
operator=3D() should be a member function. Is this understanding of mine
is correct ?

Kindly clarify.

Thanks
V.Subramanian

x+y =3D z is legal or not depends on implementation of operator+
Test Operator+(Test) // above call x+y =3D z works
void Operator+(Test t1, Test t2); // above call returns an error
.

User: "James Kanze"

Title: Re: overloaded operator=() 21 Dec 2007 06:48:55 AM
On Dec 21, 6:20 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:

overloaded operator=3D()
--------------------------------
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.

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=3D(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=3D which took all of its
parameters by value wouldn't make any sense.)

Test x;
Test y;
Test x;
x + y =3D z;
This is wrong because x + y is not an lvalue but would become
legal due to the above definition of overloaded operator=3D().
That is why operator=3D() 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=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER