title: Accessing vector of pointers in a class
body: <p>I'm trying to figure out a way to keep track of all the instances of a class I have made, so I can access them at any point using a title string (or int ID)</p>
  
  <p>I decided to use a static vector of pointers to each instance, and then on creating each instance i'd add a pointer to it to the vector.</p>
  
  <p>This works up to a point but at one point the values inside each element of the vector seem to reset/get randomly assigned values and i can't figure out what's happening.</p>
  
  <p>i'm adding the object to the vector here:</p>
  
  <pre><code>SWindow::SWindow(LPCWSTR WindowClass, LPCWSTR Title, UINT Style, int x, int y, int height, int width, HWND hParWnd, HINSTANCE hInstance)
      :
      x(x),
      y(y)
  {
      hWnd = CreateWindowEx(NULL, WindowClass, Title, Style, x, y, height, width, hParWnd, NULL, hInstance, NULL);
      SWindows.push_back(this);
  </code></pre>
  
  <p>The function at which the values change is:
  which is a member of the <code>SWindow</code> class</p>
  
  <p>SWindow.h:</p>
  
  <pre><code>static SWindow* GetSWindow(wstring ws);
  </code></pre>
  
  <p>SWindow.cpp:</p>
  
  <pre><code> SWindow* SWindow::GetSWindow(wstring ws)
  {
      for (int i = 0; i &lt; SWindow::SWindows.size(); i++)
      {
          if (SWindows[i]-&gt;title == ws)
          {
              return SWindows[i];
          }
          else
          {
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>i'm accessing the function from a different class using:</p>
  
  <pre><code>SWindow* pPlayViewer = SWindow::GetSWindow(L"Viewer");
  </code></pre>
  
  <p>Also if this is a bad way to be doing what i am trying to do, let me know of a better way.
  Thanks!</p>