title: Using C++ templates in Linked Lists, with multiple different types appearing in the list
body: <p>(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone to do my assignment for me, just to try and help me understand how to implement templates properly.)</p>
  
  <p>My current setup is:</p>
  
  <p>I have class A, which is a base class.
  Class B, C and D all children of Class A.</p>
  
  <p>I'm trying to make a linked list that, within a single list, can point to either B, C or D.</p>
  
  <p>What I currently have setup is something like this:</p>
  
  <pre><code>enum Types { TypeB, TypeC, TypeD }
  
  struct Node
  {
      void * pointerToElement;
      int type;
      Node * next;
  };
  
  struct Header
  {
      int counter;
      Node * first;
  };
  </code></pre>
  
  <p>This actually works. When I go through the linked list to print out all the elements, I use the and if statement and the <code>int type</code> to identify what kind it is (based on the ENUM defined), and then use <code>static_cast</code> to cast the void pointer to a pointer of either class B, C or D.</p>
  
  <p>Now, I've been told I'm required to use templates instead, which is causing a whole lot of headache. I haven't done much with templates, but my experience with them hasn't been all that pleasant.</p>
  
  <p>My understanding of templates is that I could use it to define the whole linked list with EITHER class B, C or D, but it wouldn't be plausible to have B, C or D all appearing in the same linked list?</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>enum Types { TypeB, TypeC, TypeD } // I realise that if templates work, I won't need this
  
  template &lt;class T&gt;
  struct Node
  {
      T * pointerToElement;
      int type;
      Node&lt;T&gt; * next;    // Reason 1 I suspect I could only use one type
  };
  
  template &lt;class T&gt;
  struct Header
  {
      int counter;
      Node&lt;T&gt; * first;    // Reason 2 I suspect I could only use one type
  };
  </code></pre>
  
  <p>The main question I have, are templates supposed to be able to do this? When implementing it in a class, I needed to specify a type for the header, which I didn't want to do, so I made that class a template as well, and it kept following through to the rest of my code which shouldn't need to be a template, and finally got to main() where I would have had to define either class B, C or D.</p>
  
  <p>Comments and suggestions appreciated.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Thanks everyone for the comments, I've probably learnt more trying this then from the lectures.</p>
  
  <p>What I've done is pretty much ditched templates, or at least the way I was trying to use them. I have used templates (unfortunately for the sake of using templates,) and it works. Here's what I've now done (all worked out from all of the helpful comments... thanks!)</p>
  
  <pre><code>template &lt;class T&gt;
  struct Node
  {
      T * pointerToElement;
      int type;   // I can get rid of this after I go through the code and remove all references to it, which I am doing now.
      Node&lt;T&gt; * next;    // Reason 1 I suspect I could only use one type
  };
  
  template &lt;class T&gt;
  struct Header
  {
      int counter;
      Node&lt;T&gt; * first;    // Reason 2 I suspect I could only use one type
  };
  </code></pre>
  
  <p>is still the way it was, but when declaring the Header, I declare it as:</p>
  
  <p>Header * myHeader;</p>
  
  <p>(I'm using a class structure identical to the one in the solution below).</p>
  
  <p>So this is pointing to the base class, the one that all the other classes are derived from. Then, because of inheritance, I can store class B, C or D in there without any problems, and providing that all the functions defined in the derived classes (B, C and D) are defined in the base class, I can call it directly without having to cast it (for example, they all have their own print function, and it calls the correct one when it is defined in the derived class).</p>
  
  <p>I think the idea that the Assignment is trying to get across is a linked list that can be used with any type, I think there was some miscommunication (probably mostly due to me,) were I thought templates were supposed to be used to define different class types in each node, rather they can be used to define the base class.</p>