title: Objects in std::vector not saving attributes correctly
body: <p>The problem I'm having is that when I change an attribute of an Object the change isn't 'saving'. Easier to show you what's happening.</p>
  
  <p>I'm learning <code>c++</code> and decided to build a small chess app. Each <code>Piece</code> is a seperate Object. </p>
  
  <p>They're stored in a <code>std::vector</code> as such</p>
  
  <pre><code>std::vector&lt;Piece&gt; pieces;
  </code></pre>
  
  <p>They're initialised like so</p>
  
  <pre><code>for (int i = 0; i &lt; 2; i++)
  {
      Piece p;
      p.Init(i*2+1, 1, renderer, SQUARE_SIZE, "king");
      pieces.push_back(p);
  }
  </code></pre>
  
  <p>When I click the mouse I want to select all pieces (temporarily)</p>
  
  <pre><code>for (int i = 0; i &lt; pieces.size(); i++)
              {
                  Piece p = pieces[i];
                  p.Select();
              }
  </code></pre>
  
  <p><strong>The issue</strong> is that while the <code>Select()</code> function is being called, by the time I get to rendering their <code>selected</code> attribute is <code>false</code>. Strangely <strong>this does not</strong> happen to the piece not contained within in the <code>vector</code>, referred to as <code>k</code>.</p>
  
  <p>Before you ask there is nowhere in my code that I set <code>selected</code> to <code>false</code> :) (Except the constructor :P )</p>
  
  <p>Also if you feel like <em>downvoting</em>, send me a comment first and I'll try fix whatever it is!</p>
  
  <p>Here are the entire files. (not sure if this is the proper way to insert them)</p>
  
  <p>Piece.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;SDL2/SDL.h&gt;
  #include &lt;SDL2/SDl_image.h&gt;
  #include &lt;array&gt;
  #include &lt;vector&gt;
  
  class Piece
  {
  public:
      Piece();
  
      void Init(int _x, int _y, SDL_Renderer* renderer, int SQUARE_SIZE, std::string type);
      void SetPos(int _x, int _y, int _w);
      void LoadTexture(SDL_Renderer* renderer, std::string type);
      void LoadMovementVector(std::string type);
      void Render(SDL_Renderer* renderer);
      void Select(){ selected = true; std::cout &lt;&lt; "called\n";}
      bool isSelected(){ return selected; }
      int GetX(){ return x; } // SDL_Point
      int GetY(){ return y; }
  
  private:
      int x, y;
      std::vector&lt;int&gt; move_vector;
      bool selected;
      SDL_Rect rect;
      SDL_Texture* texture;
  };
  </code></pre>
  
  <p>Piece.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;SDL2/SDL.h&gt;
  #include &lt;SDL2/SDl_image.h&gt;
  #include &lt;vector&gt;
  
  #include "Piece.h"
  
  Piece::Piece()
      : x(0)
      , y(0)
      , selected(false)
  {
  
  }
  void Piece::Init(int _x, int _y, SDL_Renderer* renderer, int SQUARE_SIZE, std::string type)
  {
      SetPos(_x, _y, SQUARE_SIZE);
      LoadTexture(renderer, type);
      LoadMovementVector(type);
  }
  void Piece::Render(SDL_Renderer* renderer)
  {
      //selected = true;
      //std::cout &lt;&lt; selected &lt;&lt; std::endl;
      if (selected)
      {
          SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
          SDL_RenderFillRect(renderer, &amp;rect);  
      }
      if (texture != nullptr)
      {
          SDL_RenderCopy(renderer, texture, nullptr, &amp;rect);
      }
      else 
      {
          SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
          SDL_RenderFillRect(renderer, &amp;rect);    
      }
  }
  void Piece::LoadMovementVector(std::string type)
  {
      if (type == "king"){ // There literally has to be a better way to init std::vector
          int arr[4] = {1,1,1,0};
          for (int i = 0; i &lt; 4; i++){ move_vector.push_back(arr[i]); }
      }
  
      for (int i = 0; i &lt; move_vector.size(); i++)
      {
          std::cout &lt;&lt; move_vector[i];
      }
      std::cout &lt;&lt; std::endl; 
  }
  void Piece::LoadTexture(SDL_Renderer* renderer, std::string type)
  {
      std::string source;
      if (type == "king"){
          source = "wk.png"; 
      } 
      texture = IMG_LoadTexture(renderer, "res/wk.png");
  }
  void Piece::SetPos(int _x, int _y, int _w)
  {
      x = _x; 
      y = _y; 
      rect.x = _w*(_x-1); 
      rect.y = _w*(8-_y); 
      rect.w = _w; 
      rect.h = _w; 
      std::cout &lt;&lt; x &lt;&lt; y &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;SDL2/SDL.h&gt;
  #include &lt;SDL2/SDl_image.h&gt;
  
  #include "Piece.h"
  
  using namespace std::chrono;
  
  // Would be 'const int' but I want to make the board resizeable
  int SCREEN_WIDTH = 800;
  int SCREEN_HEIGHT = 800;
  int BOARD_WIDTH, BOARD_HEIGHT, SQUARE_SIZE;
  
  SDL_Window* window;
  SDL_Renderer* renderer;
  
  std::vector&lt;Piece&gt; pieces;
  Piece k;
  
  bool InitEverything();
  bool InitSDL();
  bool CreateWindow();
  bool CreateRenderer();
  void SetupRenderer();
  void Quit();
  
  void RunGame();
  void Render();
  void HandleInput();
  void UpdateDimensions();
  
  double GetDelta();
  void RenderGameBoard(); 
  
  bool loop = true;
  auto timePrev = high_resolution_clock::now();
  
  int main(int argc, char* args[])
  {
      if (!InitEverything())
          return -1;
  
      std::cout &lt;&lt; "Running Game..." &lt;&lt; std::endl;  
      for (int i = 0; i &lt; 2; i++)
      {
          Piece p;
          p.Init(i*2+1, 1, renderer, SQUARE_SIZE, "king");
          pieces.push_back(p);
      }
      k.Init(5, 1, renderer, SQUARE_SIZE, "king");
      RunGame();
  
      Quit();
      return 0;
  }
  void RunGame()
  {
      while (loop)
      {
          HandleInput();
          Render();
  
          double delta = GetDelta();
  
      }
  }
  void Render()
  {
      SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
      SDL_RenderClear(renderer);
  
      RenderGameBoard();
  
      for (int i = 0; i &lt; pieces.size(); i++)
      {
          pieces[i].Render(renderer);
      }
      k.Render(renderer);
  
      SDL_RenderPresent(renderer);
  }
  void RenderGameBoard()
  {
      for (int i = 0; i &lt; 8; i++)
      {
          for (int j = 0; j &lt; 8; j++)
          {
              if ((j%2==0&amp;&amp;i%2==0)||(j%2!=0&amp;&amp;i%2!=0))
                  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
              else
                  SDL_SetRenderDrawColor(renderer, 180, 180, 180, 255);
              SDL_Rect r = {i*SQUARE_SIZE, j*SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE};
              SDL_RenderFillRect(renderer, &amp;r);
          }
      }
  }
  void HandleInput()
  {
      SDL_Event event;
      while (SDL_PollEvent(&amp;event))
      {
          if (event.type == SDL_QUIT)
              loop = false;
          else if (event.type == SDL_KEYDOWN)
          {
  
          }
          else if (event.type == SDL_MOUSEBUTTONDOWN)
          {
              if (event.button.button == SDL_BUTTON_LEFT)
              {
                  k.Select();
                  for (int i = 0; i &lt; pieces.size(); i++)
                  {
                      Piece p = pieces[i];
                      p.Select();
                  }
                  int x = floor(event.button.x/SQUARE_SIZE)+1;
                  int y = 8-floor(event.button.y/SQUARE_SIZE);
                  for (int i = 0; i &lt; pieces.size(); i++)
                  {
                      Piece p = pieces[i];
                      if (p.GetX() == x &amp;&amp; p.GetY() == y)
                      {
                          p.Select();
                      }
                  }
              }
          }
      }
  }
  void UpdateDimensions()
  {
      BOARD_WIDTH = SCREEN_WIDTH;
      BOARD_HEIGHT = SCREEN_HEIGHT;
      SQUARE_SIZE = BOARD_WIDTH/8;
  }
  double GetDelta()
  {
      auto timeCurrent = high_resolution_clock::now();
  
      auto timeDiff = duration_cast&lt; nanoseconds &gt;( timeCurrent - timePrev );
  
      double delta = timeDiff.count();
  
      delta /= 1000000000;
  
      timePrev = timeCurrent;
      return delta;
  }
  bool InitEverything()
  {
      if (!InitSDL())
          return false;
  
      if (!CreateWindow())
          return false;
  
      if (!CreateRenderer())
          return false;
  
      SetupRenderer();
      UpdateDimensions();
  
      return true;
  }
  bool InitSDL()
  {
      if (SDL_Init(SDL_INIT_EVERYTHING) &lt; 0)
      {
          std::cout &lt;&lt; "SDL failed to initialize : " &lt;&lt; SDL_GetError() &lt;&lt; std::endl;
          return false;
      }
      return true;
  }
  bool CreateWindow()
  {
      window = SDL_CreateWindow("Chess", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
      if (!window)
      {
          std::cout &lt;&lt; "Failed to create window : " &lt;&lt; SDL_GetError() &lt;&lt; std::endl;
          return false;
      }
      return true;
  }
  bool CreateRenderer()
  {
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
      if (!renderer)
      {
          std::cout &lt;&lt; "Failed to create renderer : " &lt;&lt; SDL_GetError() &lt;&lt; std::endl;
          return false;
      }
      return true;
  }
  void SetupRenderer()
  {
      SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
  }
  void Quit()
  {
      SDL_DestroyWindow(window);
      SDL_Quit();
  }
  </code></pre>