By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

overloaded operator=()

P: n/a
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

Dec 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Dec 21, 10:20*am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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
Dec 21 '07 #2

P: n/a
On Dec 21, 6:20 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@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
Dec 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.