Copy Assignment :: C++

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi.

I have a *pointer* to a linked list of objects. I need to write a copy assignment that will allow me to assign a new linked list the same contents as whatever the *pointer* points to.

For example:

MyList *pMList = new MyList;

pMList->insertData() // info.name1 & info.address1;
pMList->insertData() // info.name2 & info.address2;
pMList->insertData() // info.name3 & info.address3;

I would like to write a copy assignment to clone "pMList."

I have tried this:

MyList newList;

newList = *pMList;

------
const CMyList &CMyList:perator=(const CMyList &rCMyList)
{
if (&CMyList == this)
throw exception();

else
{
cloneList(CMyList);

return *this;
}
}
----------
void CMyList::cloneList(const CMyList &rSourceList)
{
cout << "\nCheck0a";

nodeCnt = rSourceList.nodeCnt;

cout << "\nCheck0b";

pOriginNode->info = rSourceList.pOriginNode->info;

CMyListNode *pNewNode = pOriginNode;

cout << "\nCheck 1";

for (CMyListNode *pTraverse = rSourceList.pOriginNode->next; pTraverse != 0; pTraverse = pTraverse->next)
{
try
{
pNewNode->next = new CMyListNode;
}

catch (bad_alloc &dmaError)
{
cerr << "\nError: " << dmaError.what[)
<< "\nPress any key to close program";

cin.ignore(256, '\n');
cin.clear();

exit(1);
}

pNewNode = pNewNode->next;
pNewNode->info = pTraverse->info;
}

cout << "\nCheck2";

pNewNode->next = 0;
}
-----

The code above looks decent. I could not find any logic error. Again, I have a *pointer* to a linked listed of objects. I need to clone that linked list. I could not get the assignment operator to work. Is there something logically incorrect?

Thanks,
Kuphryn
 

Pakaderm

Senior member
Mar 8, 2001
519
0
0
You can't use the assignment operator to copy a anything with a pointer. That's just a shallow copy, and you need to make a deep copy. You need to write a copy (or overload operator=) that walks through the list and copies each element to a new list, allocating memory as it's going.

-Pakaderm
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
I am quite certain that the crash begins here:

void CMyList::cloneList(const CMyList &rSourceList)
{ cout << "\nCheck0a";
nodeCnt = rSourceList.nodeCnt;
cout << "\nCheck0b";

-------Begin crash---------
pOriginNode->info = rSourceList.pOriginNode->info; CMyListNode *pNewNode = pOriginNode;...
---------------------------

For some reason, it does not like anything from "this" assigned to anything from rSourceList.

In other words, it does not want to be assigned, period. I am not sure if the fact that rSourceList is really a pointer to a pointer to a class (pointer->pointer->class object) has anything to do with it.

Kuphryn
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Okay. I got it to work. This is such a "minor" problem. In fact, it is my carelessness.

Here is the new revised working code:


code:
--------------------------------------------------------------------------------

void CMyList::cloneList(const CMyList &rSourceList){ cout << "\nCheck0a"; cout << "\nrSourcelist->nodeCnt = " << rSourceList.nodeCnt; nodeCnt = rSourceList.nodeCnt; cout << "\nnodeCnt = " << nodeCnt; cout << "\nCheck0b"; try { pOriginNode = new CMyList; } catch (bad_alloc &dmaError) { cerr << "\nError: " << dmaError.what() << "\nPress any key to close program"; cin.ignore(256, '\n'); cin.clear(); exit(1); } pOriginNode->eData = rSourceList.pOriginNode->eData; CSListNode *pNewNode = pOriginNode; cout << "\nCheck 1"; for (CMyList *pTraverse = rSourceList.pOriginNode->next; pTraverse != 0; pTraverse = pTraverse->next) { try { pNewNode->next = new CMyList; } catch (bad_alloc &dmaError) { cerr << "\nError: " << dmaError.what() << "\nPress any key to close program"; cin.ignore(256, '\n'); cin.clear(); exit(1); } pNewNode = pNewNode->next; pNewNode->eData = pTraverse->eData; } cout << "\nCheck2"; pNewNode->next = 0;}

--------------------------------------------------------------------------------



The change was:


code:
--------------------------------------------------------------------------------

try { pOriginNode = new CMyList; } catch (bad_alloc &dmaError) { cerr << "\nError: " << dmaError.what() << "\nPress any key to close program"; cin.ignore(256, '\n'); cin.clear(); exit(1); }

--------------------------------------------------------------------------------



I found out when I looked over the constructor. I found that there were nothing initializing pOriginNode. I then looked at the code for inserting. I saw that if CMyList was empty (pOriginNode == 0), the pOriginNode = new CMyList.

Thanks.
-----------------------

One last related question. Is it good practice to set *all* deleted pointers to right after deleting them?

Kuphryn
 

Pakaderm

Senior member
Mar 8, 2001
519
0
0
Congrats.

And yes. I usually code:

delete ptr; ptr = NULL;

That way you are safer.

-Pakaderm
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Thanks.

Yeah. I have thought about that too. I will start implementing it now.

Kuphry