title: Copy Constructor and Assignment Operator in Singly Linked List
body: <p>This is sort of a follow up to the question I asked yesterday on my assignment with singly linked lists. I've been struggling with creating the copy constructor, leaving the assignment operator until last because I think they're somewhat similar to implement. I think I have the steps down for creating the copy constructor:</p>
  
  <ol>
  <li>Create a new node with the new list as a parameter.</li>
  <li>Traverse through the list, adding each node from list1 to list2 (I have an add function which I think would help with this?)</li>
  <li>Increment through list2 while doing so, so each node gets properly added.</li>
  </ol>
  
  <p>Please correct me on any steps (or maybe I need to expand on the steps). I've looked at a lot of code found on Google in an attempt to understand what I have to do, but my implementation and understanding must still be fuzzy. Here is the code I have (the un-commented code is my last attempt. The commented code is what I've tried before, hoping that I'm at least on the right track) so far:</p>
  
  <pre><code>template&lt;typename T&gt;
  SLList&lt;T&gt;::SLList(const SLList&lt;T&gt; &amp; lst2) : head() {
      cout &lt;&lt; "Calling copy constructor..." &lt;&lt; endl;
      for (Node *ptr = lst2.head; ptr != NULL; ptr = ptr-&gt;next) {
          add(ptr-&gt;data);
      }
  }
  </code></pre>
  
  <p>Edit: Thank you WhozCraig for helping me with the copy constructor above.</p>
  
  <p>And here is the <code>add</code> function, just in case it could be used:</p>
  
  <pre><code>template&lt;typename T&gt;
  void SLList&lt;T&gt;::add(const T&amp; val) {
      Node *prev = head; 
      for (Node *ptr = head-&gt;next; ptr != NULL; ptr = ptr-&gt;next) {
          prev = ptr;
          if (ptr-&gt;data == val) {
              return;
          }
      }
      prev-&gt;next = new Node(val);
  }
  </code></pre>
  
  <p>What I'm thinking may be wrong, though is in line 5 of the first code snippet I have <code>lst2.add(ptr-&gt;data);</code> would it call the <code>add</code> function and just replace <code>ptr-&gt;data</code> with <code>val</code>? Just wondering if writing the line like that wouldn't bring up any errors.</p>
  
  <p>Edit: I forgot to post this code snippet as well. It shows me creating the two integer lists and attempting to make <code>iList2 = iList;</code> so that the copy constructor is called. I think I may have done it wrong, though.</p>
  
  <pre><code>void intList() {
      SLList&lt;int&gt; iList;
      cout &lt;&lt; "\nNow showing integer list..." &lt;&lt; endl;
      iList.add(5);
      iList.add(6);
      iList.add(6);
      iList.add(3);
      iList.add(4);
      SLList&lt;int&gt; iList2 = iList;
      iList.print();
      iList2.print();
  }
  </code></pre>
  
  <p>Edit 2: The copy constructor issue was fixed above, but now when the <code>add</code> function gets called, it crashes on the <code>for</code> loop. Could it be because <code>lst2</code> is a <code>const</code> object and <code>add</code> is not a <code>const</code> function, as WhozCraig said?</p>
  
  <p>Edit 3: My attempt at assignment operator, just in case:</p>
  
  <pre><code>template&lt;typename T&gt;
  const SLList&lt;T&gt;&amp; SLList&lt;T&gt;::operator=(const SLList &amp; rhs) {
      cout &lt;&lt; "\nCalling assignment operator..." &lt;&lt; endl;
      // Step (1) Delete the existing list
      while (head != NULL) {
          Node *temp = head;
          head = head-&gt;next;
          delete temp;
      }
      // Step (2) Build a new, copy list
      if (this != &amp;rhs) {
          for (const Node *ptr = rhs.head; ptr != NULL; ptr = ptr-&gt;next) {
              add(ptr-&gt;data);
          }
      }
      return *this;
  }
  </code></pre>