overloaded operator=()

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
 
S

Sachin

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
 
J

James Kanze

overloaded operator=()

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=(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.)
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 ?

Not at all. In fact, if operator+ returns a non-const object
(which is usually the case), then the above is legal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top