473,405 Members | 2,310 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,405 software developers and data experts.

Operator Overloading Problem

hopefully this code is not too obscure. lemme know and I can provide
clarification.

basically trying to implement a linked list (single links) manually.
the function below is supposed to assign the contents of one LL to
another but doesn't work.

I added the second last line to show what tempLL contains just prior to
its return and puzzlingly its contents are identical to those of the LL
to be assigned (rhs) - which is the expected result. however after the
return the destination LL holds nothing but nulls.

am I missing something ridiculously obvious?
// assignment operator resulting in deep copy
LinkedList LinkedList::operator=(const LinkedList& rhs)
{
LinkedList* tempLL;

if(this != &rhs)
{
tempLL = new LinkedList();
Node* cursor = rhs.firstNode;

while(cursor)
{
tempLL->AddToEnd(cursor->GetData());
cursor = cursor->GetNext();
}

}
else tempLL = this;

tempLL->Display();
return *tempLL;
}

Dec 12 '05 #1
2 1299
A_*********@hotmail.com wrote:
hopefully this code is not too obscure. lemme know and I can provide
clarification.

basically trying to implement a linked list (single links) manually.
the function below is supposed to assign the contents of one LL to
another but doesn't work.

I added the second last line to show what tempLL contains just prior to
its return and puzzlingly its contents are identical to those of the LL
to be assigned (rhs) - which is the expected result. however after the
return the destination LL holds nothing but nulls.

am I missing something ridiculously obvious?
Probably. Have you implemented the copy constructor in your class? How?
Read about "the Rule of Three".


// assignment operator resulting in deep copy
LinkedList LinkedList::operator=(const LinkedList& rhs)
{
LinkedList* tempLL;

if(this != &rhs)
{
tempLL = new LinkedList();
Node* cursor = rhs.firstNode;

while(cursor)
{
tempLL->AddToEnd(cursor->GetData());
cursor = cursor->GetNext();
}

}
else tempLL = this;

tempLL->Display();
return *tempLL;
}


V
Dec 12 '05 #2
>
// assignment operator resulting in deep copy
LinkedList LinkedList::operator=(const LinkedList& rhs)
operator= should return LinkedList &.
return *tempLL;


no it should return *this

You should implement it like this:

LinkedList & LinkedList::operator=( const LinkedList & other )
{
LinkedList temp( other );
swap( temp );
return *this;
}

and swap() should swap each member. (Make it private if you don't want
it to be part of the interface).

Dec 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
20
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
24
by: Rahul | last post by:
Hi Everyone, I was just overloading operator = for a class and i have a problem in one case... class A { A& operator=(const A& obj) { return *this;
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.