title: Adding additional vectors to a class
body: <p>Let me demonstrate my problem with an example.</p>
  
  <p>Lets say I have a bunch of very simple structs, like this:</p>
  
  <pre><code>struct A
  {
      int x;
  };
  
  struct B
  {
      float y;
      bool z;
  };
  </code></pre>
  
  <p>And I have a class that stores these structs in vectors. It has a method for adding new batch of structs (there is always the same number of each struct), and it has a method for accessing each struct by its index. Something like this:</p>
  
  <pre><code>class Test
  {
      public:
          void Create()
          {
              A a = {};
              B b = {};
              _vecA.push_back(a);
              _vecB.push_back(b);
          }
  
          A* GetA(unsigned int i)
          {
              return &amp;_vecA[i];
          }
  
          B* GetB(unsigned int i)
          {
              return &amp;_vecB[i];
          }
  
      private:
          std::vector&lt;A&gt; _vecA;
          std::vector&lt;B&gt; _vecB;
  };
  </code></pre>
  
  <p>Now, the problem is that whenever I add another struct into the program I have to add bunch of stuff into that class. For example, if I add another struct (lets call it <em>C</em>), I have to add another vector, another push_back call, and create the <em>GetC()</em> method.</p>
  
  <p>Now, my question is: Is there a better way to do that?</p>
  
  <p>Here's one way I thought of doing it:</p>
  
  <pre><code>class ContainerBase
  {
      public:
          ContainerBase(){};
          virtual ~ContainerBase(){};
          virtual void Add(){};
  };
  
  template &lt;typename T&gt;
  class Container : public ContainerBase
  {
      public:
              // Singleton
              static Container&amp; Instance()
              {
                  static Container instance;
                  return instance;
              }
  
              void Add()
              {
                  T t = {};
                  _bag.push_back(t);
              }
  
              T* Get(unsigned int i)
              {
                  return &amp;_bag[i];
              }
  
          private:
              Container(){};
              ~Container(){};
  
              std::vector&lt;T&gt; _bag;
  };
  
  class Test
  {
      public:
          template &lt;typename T&gt;
          void Register()
          {
              _containers.push_back(&amp;Container&lt;T&gt;::Instance());
          }
  
          void Create()
          {
              for (unsigned int i = 0; i &lt; _containers.size(); i++)
                  _containers[i]-&gt;Add();
          }
  
          template &lt;typename T&gt;
          T* Get(unsigned int i)
          {
              return Container&lt;T&gt;::Instance().Get(i);
          }
  
      private:
          std::vector&lt;ContainerBase*&gt; _containers;
  };
  </code></pre>
  
  <p>I tried to solve it by creating a templated class <em>Container</em> for managing the vector of each type. As you can see, it involves using a singleton pattern to make sure that there's always a <em>Container</em> for the particular type of struct. The types of structs just need to be registered with the class using the <em>Register</em> method, so that <em>Test</em> knows which <em>Containers</em> it should add the new structs to, when the new batch is created. This solution is problematic because it introduces global state to the containers, and by doing that makes it impossible to create two instances of the <em>Test</em> class that don't share their vectors for a certain type of structs. If I don't make the containers singletons, than there is no way I could access them by type in the <em>Test</em>'s Get function, I would have to know which index in the _<em>containers</em> vector corresponds to what type of container.</p>
  
  <p>I feel that there should be a better way to do this.</p>