title: Store pointers of different type in one array
body: <p>I want to store pointers to objects of different classes in one data-structure ( such as array, HashMap ... ). According to <a href="https://stackoverflow.com/questions/9526226/storing-multi-type-data-in-untyped-memory-space">this answer</a> I came out to following solution using untyped pointer <code>void*</code> and operator <code>&amp;*( ClassName* )</code> for pointer type recasting:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  class ClassA{ public: int    a;  };
  class ClassB{ public: double b;  };
  class Storage{
      public:
      int n;
      void** store;
      Storage( int n_ ){ n = n_; store = new void*[n]; };
  };
  Storage s( 5 );
  int main(){
      // test store and load ClassA
      ClassA a1; a1.a = 16;           
      s.store[ 0 ] = &amp;a1;
      ClassA* a2 = &amp;*( ClassA* ) s.store[ 0 ];
      printf(  " a2 %i \n", a2-&gt;a );
      // test store and load ClassB
      ClassB b1; b1.b = 15.1455;
      s.store[ 1 ] = &amp;b1; 
      ClassB* b2 = &amp;*( ClassB* ) s.store[ 1 ];
      printf(  " b2 %f \n", b2-&gt;b );
  }
  </code></pre>
  
  <p>Now <strong>questions</strong>:</p>
  
  <ol start="2">
  <li>Is anything bad about typecasting like <code>&amp;*( ClassB* )</code> except that it is not type safe?</li>
  <li>Is there any performance cost for this typecasting ?</li>
  <li>Is there any better ( nicer, more safe, faster ) way how store pointer to different objects of different class in a single array?</li>
  </ol>
  
  <hr>
  
  <p>Ah, I figured out that it is actually not usefull to make it <code>void**</code> because than I have now way how to recognize type of the stored object later. Better would be do it with template, and common super class. Like this</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;cstdlib&gt;
  template&lt; class TYPE1 &gt; class Storage{
      public:
      int n;
      TYPE1** store;
      Storage( int n_ ){ n = n_; store = new TYPE1*[n];     };  // Constructor
      void   put( int key, TYPE1* obj ){ store[key] = obj;  };  // save data
      TYPE1* get( int key             ){ return store[key]; };  // load data
  };
  class Parent { public: int    kind;                                                                     };
  class ChildA : public Parent { public: int    someData;   ChildA( int    someData_  ){ kind=0; someData =someData_;  }  };
  class ChildB : public Parent { public: double otherData;  ChildB( double otherData_ ){ kind=1; otherData=otherData_; }  };
  Storage&lt;Parent&gt; mixed( 10 );
  int main(){
      srand( 15454 );
      printf( " ==== initialize by random data and radom type \n" ); 
      for(int i=0; i&lt;mixed.n; i++ ){ 
          if   ( (rand()&amp;4) &gt; 0 ){ 
              int someData = rand()&amp;0xFF; 
              mixed.put( i, new ChildA(  someData ) );
              printf( " i %i ChildA.someData  = %i \n", i , someData );    
          }else{  
              double otherData =  (rand()&amp;0xFF)/256.0f; 
              mixed.put( i, new ChildB(  otherData ) );
              printf( " i %i ChildB.otherData = %f \n", i , otherData );  
          };
      };
      printf( " ==== recall stored data \n" ); 
      for(int i=0; i&lt;mixed.n; i++ ){ 
          Parent* obj = mixed.get( i );
          if   ( obj-&gt;kind ==0 ){ printf( " i %i ChildA.someData  = %i \n", i , ( (ChildA *) obj )-&gt;someData  ); }
          else                  { printf( " i %i ChildB.otherData = %f \n", i , ( (ChildB *) obj )-&gt;otherData ); };
      };
  }
  </code></pre>