Thread: about link list - - please help

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    about link list - - please help

    can someone give me an idea on how to deep copy..
    this is my solution but i dont know am i right.. can someone correct it pleae....

    Code:
    IPlist::IPlist (const IPlist & other)
    //copy constructor to create a deep copy of other IPlist object
    {
        nodeType *tmp = new nodeType;
        tmp->link = NULL;
        while(other != NULL)
        {
            tmp->lnk = other->link;
            tmp = other;
            other = other->link;
        }
    }

  2. #2
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    I recommend you use a stl container instead of a linked list.

    Have you copy constructor call an the assignment operator. Ensure that the assignment operator does not permit a copy of an object to itself. Do the 'deep' copy within the assignment operator.

    Your example requires a copy constructor and assignment operator for both IPList and NoseList.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you copy constructor call an the assignment operator.
    The opposite is probably better. Have the copy assignment operator call the copy constructor, possibly with a custom swap member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight
    The opposite is probably better. Have the copy assignment operator call the copy constructor, possibly with a custom swap member function.
    If you're considering using the constructor from within the assignment operator then you must read this: http://www.gotw.ca/gotw/023.htm

    Actually, since you mentioned swap, you're actually suggesting that his copy-constructor should be a move-constructor, which is also a problem.


    If the intention is to copy the entire list, how could it posibly work when new is only called for one node?
    Try again writing this and put 'new' in the loop. Do it all on paper as you go because that will probably help a lot.

    The code posted also cannot compile for a number of reasons, like using -> with a reference.
    Last edited by iMalc; 10-20-2006 at 12:51 AM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by iMalc
    If you're considering using the constructor from within the assignment operator then you must read this: http://www.gotw.ca/gotw/023.htm

    Actually, since you mentioned swap, you're actually suggesting that his copy-constructor should be a move-constructor, which is also a problem.


    If the intention is to copy the entire list, how could it posibly work when new is only called for one node?
    Try again writing this and put 'new' in the loop. Do it all on paper as you go because that will probably help a lot.

    The code posted also cannot compile for a number of reasons, like using -> with a reference.
    you mean put another node in the loop? or move tmp into the loop?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> http://www.gotw.ca/gotw/023.htm
    The example used in that link is not what laserlight had in mind. Instead, the idea is to copy construct a temporary inside the copy assignment operator, then swap the temporary with the local object (again inside the assignment operator). I believe this technique is actually recommended by Sutter in C++ Coding Standards, and it does not use a move constructor.
    Last edited by Daved; 10-20-2006 at 08:50 AM.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    this is an example from that website that Daved gave. is this useful for what i need???

    Code:
    [Example:
        struct C {
          int i;
          void f();
          const C& operator=( const C& );
        };
        const C& C::operator=( const C& other)
        {
          if ( this != &other )
          {
            this->~C();     // lifetime of '*this' ends
            new (this) C(other);
                            // new object of type C created
            f();            // well-defined
          }
          return *this;
        }
        C c1;
        C c2;
        c1 = c2; // well-defined
        c1.f();  // well-defined; c1 refers to
                 //  a new object of type C
      --end example]

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, that's the link iMalc gave. And no, it is not what you want to do at all. That is an example of what not to do, and the link says to "never write" code like that.

    You can use the do_copy() example in that link. Or, as laserlight suggested, you can use a swap method to assist in implementing the copy assignment operator. For an example of that, see the answer to "2. What is the canonical form of strongly exception-safe copy assignment?" in: http://www.gotw.ca/gotw/059.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM