title: Destructor for a List class in C++
body: <p>I'm new to C++ thus the question. I've a toy implementation of a Singly Linked List in C++.</p>
  
  <pre><code>template&lt;typename T&gt;
  class List {
      template&lt;typename U&gt;
      struct Node {
          U data_;
          Node&lt;U&gt;* next_;
  
          Node() : data_(0), next_(nullptr) {}
          Node(U data) : data_(data), next_(nullptr) {}
      };
  
  private:
      Node&lt;T&gt;* head_;
      std::size_t size_;
  
  public:
      List() : head_{nullptr}, size_{0} {}
  
      void insert(const T&amp; item) {
          Node&lt;T&gt;* p(new Node&lt;T&gt;(item));
          if (size_ == 0) {
              head_ = p;
          } else {
              p-&gt;next_ = head_;
              head_ = p;
          }
          size_++;
      }
      std::size_t getSize() {
          return size_;
      }
  
      ~List(){
      while(head_){
          Node&lt;T&gt; p = head_;
          delete(p);
          head_ = head_-&gt;next_;
      }
  };
  </code></pre>
  
  <p>This code seems to work.  The problem though is that the objects allocated by <code>new</code> are never cleaned up, despite the <code>~List()</code> destructor. Can someone help me understand, how I can write a destructor for this class that cleans up all the allocated nodes ?</p>
  
  <p><strong>Important remark:</strong> I am aware that this can be done using smart pointers, but I want to understand the old school way of managing heap.</p>