title: How to understand freelist implementation for linked-list in C++?
body: <p>I don't understand the last few lines here.</p>
  
  <pre><code>// Singly linked list node with freelist support
  template&lt;typename Elem&gt; class Link {
  private:
    static Link&lt;Elem&gt;* freelist; // Reference to freelist head
  public:
    Elem element;                // Value for this node
    Link* next;                  // Point to next node in list
  
    // Constructors
    Link(const Elem&amp; elemval, Link* nextval = NULL)
      { element = elemval; next = nextval; }
    Link(Link* nextval = NULL) { next = nextval; }
  
    void* operator new(size_t) { // Overloaded new operator
      if (freelist == NULL) return ::new Link; // Create space
      Link&lt;Elem&gt;* temp = freelist; // Can take from freelist
      freelist = freelist-&gt;next;
      return temp;                 // Return the link
    }
  
    // Overloaded delete operator
    void operator delete(void* ptr) {
      ((Link&lt;Elem&gt;*)ptr)-&gt;next = freelist; // Put on freelist
      freelist = (Link&lt;Elem&gt;*)ptr;
    }
  };
  
  // The freelist head pointer is actually created here
  template&lt;typename Elem&gt;
  Link&lt;Elem&gt;* Link&lt;Elem&gt;::freelist = NULL;
  </code></pre>
  
  <p>In particular, why do we need to define a pointer to a pointer in the last line? <code>Link&lt;Elem&gt;::freelist</code> is already a pointer, so why do we need to set another pointer to that pointer to <code>NULL</code>?</p>
  
  <p>Can someone clear this up for me? I'm very confused.</p>