Thread: Copy Assignment :: C++

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    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: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

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    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.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    I need to clone the linked list, not have it point to the same location as the source linked list.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. default implementation of assignment operator
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2007, 12:49 AM
  2. Doing maths to copy constructed variables
    By Paul Skinner in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 05:31 PM
  3. Copy constructor for Queue ADT (as an Array)
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2007, 11:05 PM
  4. containers/ assignment operator
    By Kirdra in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2004, 10:17 AM
  5. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 05:17 PM