title: Generic class for dynamic arrays in c++
body: <p>Is it possible to make a generic class that acts as a dynamic array of objects of any type?
  I want a class that basically does this:</p>
  
  <pre><code>MyObj * obj1 = new MyObj();
  MutableArray * arr = new MutableArray();
  arr-&gt;addObject(obj1);
  MyObj * obj2 = arr-&gt;objectAtIndex(0);
  // obj1 and obj2 now points to the same object
  </code></pre>
  
  <p>This is what the code would look like. I know this doesn't work, but you get the idea. 
  What I need is some generic type for an object. The array itself just consists of pointers so the size of the object shouldn't matter, right?</p>
  
  <p>So, is this possible?</p>
  
  <p><strong>.h-file</strong></p>
  
  <pre><code>class MutableArray
  {
      private:
          class * objs;
          int length;
          int capacity;
  
      public:
          MutableArray();
          void add(class * obj);
          class objectAtIndex(int index);
  };
  </code></pre>
  
  <p><strong>cpp-file</strong></p>
  
  <pre><code>MutableArray::MutableArray()
  {
      length = 0;
      capacity = 0;
  }
  
  void MutableArray::add(class * obj)
  {
      if(length &gt;= capacity)
      {
          this-&gt;enlarge();
      }
      objs[length] = obj;
      length++;
  }
  
  void MutableArray::enlarge()
  {
      int newCapacity = (capacity * 2)+1;
      class * newObjs = new class[newCapacity]
  
      if(capacity != 0)
      {
          delete [] objs;
      }
  
      objs = newObjs;
      capacity = newCapacity;
  }
  
  class MutableArray::objectAtIndex(int index)
  {
      return objs[index];
  }
  </code></pre>