title: Dynamically allocated list in C++
body: <p>I made a cute generic (i.e. template) <code>List</code> class to handle lists in C++. The reason for that is that I found the <code>std::list</code> class terribly ugly for everyday use and since I constantly use lists, I needed a new one. The major improvement is that with my class, I can use <code>[]</code> to get items from it. Also, still to be implemented is an <code>IComparer</code> system to sort things.</p>
  
  <p>I'm using this <code>List</code> class in <code>OBJLoader</code>, my class that loads Wavefront .obj files and converts them to meshes. <code>OBJLoader</code> contains lists of pointers to the following "types": 3D positions, 3D normals, uv texture coordinates, vertices, faces and meshes. The vertices list has objects that must be linked to some objects in all of the 3D positions, 3D normals and uv texture coordinates lists. Faces link to vertices and meshes link to faces. So they are all inter-connected. </p>
  
  <p>For the sake of simplicity, let's consider that, in some context, there are just two lists of pointers: <code>List&lt;Person*&gt;</code> and <code>List&lt;Place*&gt;</code>. <code>Person</code> class contains, among others, the field <code>List&lt;Place*&gt; placesVisited</code> and the <code>Place</code> class contains the field <code>List&lt;Person*&gt; peopleThatVisited</code>. So we have the structure:</p>
  
  <pre><code>class Person
  {
      ...
    public:
      Place* placeVisited;
      ...
  };
  
  class Place
  {
      ...
    public:
      List&lt;People*&gt; peopleThatVisited;
  };
  </code></pre>
  
  <p>Now we have the following code:</p>
  
  <pre><code>Person* psn1 = new Person();
  Person* psn2 = new Person();
  
  Place* plc1 = new Place();
  Place* plc2 = new Place();
  Place* plc2 = new Place();
  
  
  // make some links between them here:
  psn1-&gt;placesVisited.Add(plc1, plc2);
  psn2-&gt;placesVisited.Add(plc2, plc3);
  
  // add the links to the places as well
  plc1-&gt;peopleThatVisited.Add(psn1);
  plc2-&gt;peopleThatVisited.Add(psn1, psn2);
  plc3-&gt;peopleThatVisited.Add(plc3);
  
  // to make things worse:
  
  List&lt;Person*&gt; allThePeopleAvailable;
  
  allThePeopleAvailable.Add(psn1);
  allThePeopleAvailable.Add(psn2);
  
  List&lt;Place*&gt; allThePlacesAvailable;
  
  allThePlacesAvailable.Add(plc1);
  allThePlacesAvailable.Add(plc2);
  allThePlacesAvailable.Add(plc3);
  </code></pre>
  
  <p>All done. What happens when we reach <code>}</code>? All the dtors are called and the program crashes because it tries to delete things two or more times.</p>
  
  <p>The dtor of my list looks like this:</p>
  
  <pre><code>~List(void)
  {
      cursor = begin;
      cursorPos = 0;
  
      while(cursorPos &amp;#60; capacity - 1)
      {
          cursor = cursor-&gt;next;
          cursorPos++;
          delete cursor-&gt;prev;
      }
  
      delete cursor;
  }
  </code></pre>
  
  <p>where <code>Elem</code> is:</p>
  
  <pre><code>struct Elem
  {
    public:
      Elem* prev;
      T value;
      Elem* next;
  };
  </code></pre>
  
  <p>and <code>T</code> is the generic <code>List</code> type.</p>
  
  <p>Which brings us back to the question: What ways are there to safely delete my <code>List</code> classes? The elements inside may or may not be pointers and, if they are pointers, I would like to be able, when I delete my <code>List</code>, to specify whether I want to delete the elements inside or just the <code>Elem</code> wrappers around them. </p>
  
  <p>Smart pointers could be an answer, but that would mean that I can't have a <code>List&lt;bubuType*&gt;</code>, but just <code>List&lt;smart_pointer_to_bubuType&gt;</code>. This could be ok, but again: declaring a <code>List&lt;bubuType*&gt;</code> would cause no error or warning and in some cases the smart pointers would cause some problems in the implementation: for example, I might want to declare a <code>List&lt;PSTR&gt;</code> for some WinAPI returns. I think getting those <code>PSTR</code> inside smart pointers would be an ugly job. Thus, the solution I'm looking for I think should be somehow related to the deallocation system of the <code>List</code> template.</p>
  
  <p>Any ideas?</p>