473,379 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

Dec 21 '07 #1
2 2079
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
14
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast ,...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.