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