-
Copy Assignment :: C++
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::operator=(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
-
I didnt read through your code but if you want to just copy one pointers location to another you simply pass its memory address into it.
eg if your original pointer is called orig_point
and the newone called new_point
You would copy it by
new_point = &orig_point
This gives new_point to the memory address that orig_point to pointing to.
-
Thanks.
I need to clone the linked list, not have it point to the same location as the source linked list.
Kuphryn
-
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 CSList::cloneList(const CSList &rSourceList)
{
cout << "\nCheck0a";
cout << "\nrSourcelist->nodeCnt = " << rSourceList.nodeCnt;
nodeCnt = rSourceList.nodeCnt;
cout << "\nnodeCnt = " << nodeCnt;
cout << "\nCheck0b";
try
{
pOriginNode = new CSListNode;
}
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 (CSListNode *pTraverse = rSourceList.pOriginNode->next; pTraverse != 0; pTraverse = pTraverse->next)
{
try
{
pNewNode->next = new CSListNode;
}
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:
------------------------------------------------------------------------
try
{
pOriginNode = new CSListNode;
}
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