title: C++ Linked List Deep Copy Constructor and Assignment Overloaded
body: <pre><code>#ifndef LIST
  #define LIST
  
   #include &lt;iostream&gt;
   #include &lt;cstdlib&gt;
   #include &lt;string&gt;
  
   using namespace std;
  
  typedef string ElementType;
  
  
   class List
  {
    private:
  
   class Node
   {
  
   public:
    ElementType data;
    Node * next;
  
    Node()
    :data(ElementType()), next(NULL)
    {}
  
    Node(ElementType initData)
    :data(initData), next(NULL)
    {}
  
  }; // end of Node class         
  
  typedef Node * NodePointer;
  
  
  
    public:
    private:
    NodePointer first;
    int mySize;
    }; // end of List class
  </code></pre>
  
  <p>This is my overloaded assignment operator, im not sure what im doing wrong, but im beyond frustrated,I've searched the web and read different forums and cannot find a solution to do this, could someone please push me in the right direction, I left out all my public member functions, because none of them operate on my overloaded assignment and copy constructor, I just wanted you guys to get an idea of my layout</p>
  
  <p>overloaded assignment operator</p>
  
  <pre><code>List &amp; List::operator=(const List &amp;rightSide)
  {
  if(this != &amp;rightSide)
  {
      this-&gt;~List();
  }
  
  NodePointer ptr = rightSide.first;
  NodePointer cptr = ptr;
  
  while(ptr != NULL)
  {   
      cptr-&gt;next = new Node(ptr-&gt;data);
      cptr = cptr-&gt;next;
      ptr = ptr-&gt;next;
  }
  
  return *this;   
  </code></pre>
  
  <p>}</p>
  
  <p>this is my copy constructor</p>
  
  <pre><code>List::List(const List &amp;source)
  {
  NodePointer ptr = source.first;
  NodePointer cptr;
  
  if(ptr == NULL)
  {
      cerr &lt;&lt; "Bad allocation, empty list" &lt;&lt; endl;
      exit(1);
  }
  
  while(ptr != NULL)
  {
      cptr = new Node(ptr-&gt;data);
      cptr-&gt;next =    ptr-&gt;next;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
      ptr = ptr-&gt;next;
   }
   }
  </code></pre>