title: Creating/managing a heterogenous container(c++)
body: <p>I'm having a problem with correctly building a container that stores class specimens of different types that are all inheritors of a single abstract class. The register(the container) stores a pointer to the array of these specimens, that has the type of the abstract class. Whenever I try to access data contained in the specimens I only succeed in retrieving parts that can be found in the base class too. For example, an overloaded &lt;&lt; used on the register that contains elements of all three inheritors, will only write the abstract class parts on screen, and will neglect anything not present there. Now I don't really know if the problem is with printing out the otherwise correctly stored elements, or the storing is already done in an inappropriate form, so that would be my question: how should this be done properly? Here's the code:</p>
  
  <pre><code>class Register{
  private:
  int elementNum;
  type * pData;
  friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp; os,const Register &amp;v);
  };
  class type{
  int a;
  int b;
  };
  class type2: public type{
  int c;
  int d;
  };
  </code></pre>
  
  <p>The other two inheritors behave the same way as the type2. Here's a part of main:</p>
  
  <pre><code>    int main ()
      {
          type2 A1(1,2,3,4);
          type3 D1(4,5,6,7,8);
          type4 H1(9,10,11,12,13);
          std::cout&lt;&lt;A1&lt;&lt;D1&lt;&lt;H1&lt;&lt;endl;
          Register R1;
          R1.Add(0,A1);
          R1.Add(1,D1);
          R1.Add(2,H1);
          R1.Display();
          R1.MaxLength();
          std::cout&lt;&lt;R1;
          return 0;
      }
  </code></pre>
  
  <p>Operator &lt;&lt; on the register:</p>
  
  <pre><code>std::ostream &amp;operator&lt;&lt;(std::ostream &amp; os,const Register &amp;v){
      for(int i=0;i&lt;v.elementNum;i++)
      {
          os&lt;&lt;v.pData[i]&lt;&lt;endl;
      }
      return os;
  }
  </code></pre>
  
  <p>Only using the &lt;&lt; operator or a function from the register ends in this problem.
  Edit: Implementation of the Add function:</p>
  
  <pre><code>void Register::Add(int position,type&amp; T){
      if(position&lt;0||position&gt;elementNum+1)
          return;
      type *pTemp = new type[elementNum+1];
      if(elementNum==0)
      {
          pTemp[0]=T;
          delete[]pData;
          pData=pTemp;
      }
      else
      {
          for(int i=0,j=0;j&lt;elementNum+1;i++,j++)
          {
              if(position!=j)
                  pTemp[j]=pData[i];
              else
              {
                  i--;
                  pTemp[j]=a;
              }
          }
          delete[]pData;
          pData=pTemp;
      }
      elementNum++;
  }
  </code></pre>