title: How can implement a C++ vector that points to other, multiply typed vectors?
body: <p>I want to store elements of multiple types in a single vector, while keeping elements of the same type <strong>contiguous</strong>. The types are derived from a base class and I expect different types to be implemented throughout the development cycle. For this reason it would help if the process of adding a new type to the list is very straightforward. </p>
  
  <p>I can achieve this (to an extent) in the following manner:</p>
  
  <pre><code>//header
  enum TypeID { TypeA_ID, TypeA_ID, TypeA_ID, TypeIDAmount };
  
  vector&lt;TypeA&gt; vectorA;
  vector&lt;TypeB&gt; vectorB;
  vector&lt;TypeC&gt; vectorC;
  
  //cpp
  TypeBase* LookUp(TypeID type, int index)
  {
      switch(type)
      {
      case TypeA_ID: return (TypeBase*) &amp;vectorA[index];
      case TypeB_ID: return (TypeBase*) &amp;vectorB[index];
      case TypeC_ID: return (TypeBase*) &amp;vectorC[index];
      }
  }
  </code></pre>
  
  <p>However this is not clean, easy to maintain, nor compile-friendly (the class holding the data is included in many places).</p>
  
  <p>A more compile friendly (but uglier) option I thought was doing something like this</p>
  
  <pre><code>//header
  void* vectorArray;
  
  //cpp
  void Initialize()
  {
      vectorArray = new void*[TypeIDAmount];
      vectorArray[0] = new vector&lt;TypeA&gt;;
      vectorArray[1] = new vector&lt;TypeB&gt;;
      vectorArray[2] = new vector&lt;TypeC&gt;;
  }
  
  TypeBase* LookUp(TypeID type, int index)
  {
      void* pTypedVector = &amp;vectorArray[type];
      switch(type)
      {
      case TypeA_ID: return (TypeBase*) (*(vector&lt;TypeA&gt;*)pTypedVector)[index];
      case TypeB_ID: return (TypeBase*) (*(vector&lt;TypeB&gt;*)pTypedVector)[index];
      case TypeC_ID: return (TypeBase*) (*(vector&lt;TypeC&gt;*)pTypedVector)[index];
      }
  }
  </code></pre>
  
  <p>(ew!)</p>
  
  <p>Is there anything that would work generically like this?</p>
  
  <pre><code>vector&lt; vector&lt;?&gt;* &gt; vectorOfVariedVectors;
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>The motivation for this structure is to store components in the Entity-Component design pattern. </p>
  
  <p>The reason I want the Types (or rather, components) to be contiguous, is to be able to transverse them in a cache-friendly way. This means that I want the instances themselves to be contiguous. While using contiguous pointers would give me a behaviour similar to what I want, if they point to 'random' places in memory the cache misses will still happen when fetching their data. </p>
  
  <p>Avoiding memory fragmentation is a nice additional benefit of this.</p>
  
  <p>The main idea is to have a clean manager type class that holds and provides access to these elements. Having multiple member vectors that other developers would have to add to this class is undesirable as long as this requires users that create new classes to alter this manager class. Edits to this container class should be as simple as possible or hopefully, non-existent.</p>
  
  <p><strong>Solution Found</strong></p>
  
  <p>Thanks to Dmitry Ledentsov for pointing me to <a href="http://bannalia.blogspot.ca/2014/05/fast-polymorphic-collections.html" rel="nofollow" title="Fast polymorphic collections">this article</a>. This is pretty much what I was looking for.  </p>