title: Data Structure to hold multiple data types C++
body: <p>In my application there is a class (i.e. ItemData Class) which has more than 30 member variables of different types such as</p>
  
  <pre><code>int a;
  int b;
  std::string c;
  float d;
  double e;
  double f;
  char * g;
  </code></pre>
  
  <p>and many more.</p>
  
  <p>My application needs to create large number of itemdata classes so that memory usage of the application is high. One special fact about this class is most of the instances of ItemData class has values for few member variables only.</p>
  
  <p>Ex: Instance one may have values for a and b only. Instance two can have values for b,c and g only.</p>
  
  <p>So to reduce the memory usage of my application, I need way to allocate memory only for member variables which has data when the instance is creating. 
  So I though of having generic data structure which can access elements via position and store data on that. So ItemData class has something like below and store dynamically allocated data on that.(I have to maintain the position and information hold on each position seperately)</p>
  
  <pre><code>std::vector&lt;void*&gt; vec_DataArray;
  </code></pre>
  
  <p>If ItemData instance mItemData1 has values for a and d:</p>
  
  <pre><code>mItemData1.vec_DataArray[0] = new int(iValue);
  mItemData1.vec_DataArray[3] = new float(fValue);
  </code></pre>
  
  <p>Can someone let me know whether this is a good approach to reduce the memory usage in my application? Is there any generic container which can hold multiple data types (for vec_DataArray) so  that I can avoid void* to datatype conversion when accessing data.</p>