Connecting Tech Pros Worldwide Help | Site Map

overloaded operator=()

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 21st, 2007, 04:25 AM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
Default 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


  #2  
Old December 21st, 2007, 05:05 AM
Sachin
Guest
 
Posts: n/a
Default 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
  #3  
Old December 21st, 2007, 11:55 AM
James Kanze
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.