Copy Assignment :: C++

Status
Not open for further replies.
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::eek: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
 
Sure.

code:<HR>
Code:
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;
}

Kuphryn
 
For future use, idented & cleaned the lower part of the code:
code:<HR>
Code:
const CMyList &CMyList::operator=(const CMyList &rCMyList)
{
    if (&CMyList == this)
        throw exception();
    else
    {
        cloneList(CMyList);
        return *this;
    }
}
----------
void CMyList::cloneList(const CMyList &rSourceList)
{
    nodeCnt = rSourceList.nodeCnt;

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

    CMyListNode *pNewNode = pOriginNode;

    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;
    }
    pNewNode->next = 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
 
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:<HR>
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:<HR>
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
 

earl

Well-known member
3,066
It is very good practice to set all pointers to NULL after deleting them. This drastically raises the chance that a dereference of a deleted pointer will cause a crash ASAP after the deletion, making debugging much simpler. Finding bad memory references this way is sloppy, but nice when you are developing. It's better that it crash on your computer than on a users'...

-earl-
 
Status
Not open for further replies.