title: C++ Assignment of boolean condition values within if statement not working?
body: <p>I've been coding a game in C++ using the SDL library. Today while changing the way my player character class works, I've come up against a very puzzling problem. The following code forms part of my logic allowing the player to fire bullets. The control variables, b_canFire and b_shouldFire (I plan to rename these to make more sense), are set elsewhere in the class to allow this function to execute when the user presses a key. </p>
  
  <pre><code>bool PlayerChar::DoFiring()
  {
      if(b_canFire &amp;&amp; b_shouldFire)
      {
          Fire(box.x + 22, box.y); // This fires a bullet
          b_canFire = false; // Does not work
          b_shouldFire = false; // Does not work
  
          return true;
      }
  }
  </code></pre>
  
  <p>When I step through this code using my debugger, it becomes aparrent that the values of b_canFire and b_shouldFire are not being changed to false by the assignments inside the if statement. However, if I change the code to the following:</p>
  
  <pre><code>bool PlayerChar::DoFiring()
  {
      if((b_canFire) &amp;&amp; (b_shouldFire))
      {
          Fire(box.x + 22, box.y); // This fires a bullet
          b_canFire = false; // Works
          b_shouldFire = false; // Works
  
          return true;
      }
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>bool PlayerChar::DoFiring()
  {
      if(b_canFire == true &amp;&amp; b_shouldFire == true)
      {
          Fire(box.x + 22, box.y); // This fires a bullet
          b_canFire = false; // Works
          b_shouldFire = false; // Works
  
          return true;
      }
  }
  </code></pre>
  
  <p>Suddenly the assignments work. I have also tried replicating this situation in an empty test-project, as follows:</p>
  
  <pre><code>bool bC = true;
  bool bD = true;
  
  if(bC == true &amp;&amp; bD == true)
  {
      bC = false; // Works
      bD = false; // Works
  }
  
  
  bool bE = true;
  bool bF = true;
  
  if(bE &amp;&amp; bF)
  {
      bE = false; // Works
      bF = false; // Works
  }
  </code></pre>
  
  <p>However, both of these examples assign the values exactly as they should. Clearly I am missing something here, but for the life of me, I can't see what it is. I have figured out how to fix the problem to make my code work, but it's really bothering me not knowing what is breaking the assignments in the first example, because everything I've learned about C++ so far is telling me they should work fine. </p>
  
  <p>This is my first major project using the C++ language, and I am still learning, so any help or advice from more experienced programmers would be great.</p>
  
  <p>Thanks!</p>
  
  <p>EDIT:</p>
  
  <p>As requested here is the entire class:</p>
  
  <pre><code>#include &lt;list&gt;
  #include "SDL_mixer.h"
  #include "hiGlobalVars.h"
  #include "hiGlobalObjects.h"
  #include "hiAssetManager.h"
  #include "hiRendering.h"
  #include "hiTimer.h"
  #include "hiBullet.h"
  #include "hiPlayerChar.h"
  #include "hiDebugger.h"
  
  
  using std::list;
  
  
  PlayerChar::PlayerChar()
  {
      moveSpeed = 6;
      moveDir = NONE;
      b_canFire = true;
      b_shouldFire = false;
      box.x = 0;
      box.y = 470;
      box.w = 38;
      box.h = 40;
  }
  
  
  PlayerChar::~PlayerChar()
  {
  
  }
  
  
  void PlayerChar::SetPos(int x)
  {
      box.x = x;
  }
  
  
  void PlayerChar::Draw()
  {
      BlitSurface(box.x, box.y, assets.ss_playerchar_idle, ss_screen);
  }
  
  
  bool PlayerChar::DoFiring()
  {
      if((b_canFire) &amp;&amp; (b_shouldFire)) 
      {
          // Fire a bullet
          Fire(box.x + 22, box.y);
          b_canFire = false;
          b_shouldFire = false;
  
          return true; // fired a bullet
      }
      return false; // did not fire a bullet
  }
  
  
  void PlayerChar::Fire(int x, int y)
  {
      // Create a new bullet at the correct location and add it to the global bullet list
      Bullet* bullet = new Bullet();
      bullet-&gt;SetPos(x, y);
      bullets.push_back(bullet);
  
      // Play bullet firing sound
      Mix_PlayChannel(-1, assets.mc_firebullet, 0);
  }
  
  
  void PlayerChar::HandleInput(Uint8* keystates)
  {
      if(keystates[SDLK_LEFT] &amp;&amp; keystates[SDLK_RIGHT])// Both direction keys
          moveDir = NONE;
      if(keystates[SDLK_LEFT] &amp;&amp; !keystates[SDLK_RIGHT]) // Left key and not right key
          moveDir = LEFT; 
      if(!keystates[SDLK_LEFT] &amp;&amp; keystates[SDLK_RIGHT]) // Right key and not left key
          moveDir = RIGHT; 
      if(!keystates[SDLK_LEFT] &amp;&amp; !keystates[SDLK_RIGHT]) // Neither direction key
          moveDir = NONE;
  
      if(keystates[SDLK_SPACE]) // Space bar
          b_shouldFire = true;
  
      if(!keystates[SDLK_SPACE]) // Allow another bullet to be fired after release
          b_canFire = true;
  }
  
  
  void PlayerChar::Move()
  {
      if(moveDir == LEFT &amp;&amp; box.x &gt; 0) // If not off screen, move
          box.x -= moveSpeed;
      if(moveDir == RIGHT &amp;&amp; box.x &lt; (ss_screen-&gt;w - box.w)) 
          box.x += moveSpeed;
  }
  </code></pre>
  
  <p>and the header:</p>
  
  <pre><code>#ifndef __hiPlayerChar__
  #define __hiPlayerChar__
  
  
  #include "SDL.h"
  #include "SDL_mixer.h"
  #include "hiGlobalVars.h"
  
  
  enum MoveDir
  {
      LEFT,
      RIGHT,
      NONE
  };
  
  
  class PlayerChar
  {
  private:
      Sint16 moveSpeed;
      MoveDir moveDir;
      bool b_canFire;
      bool b_shouldFire;
  
  public:
      // Ctr &amp; Dtr
      PlayerChar();
      ~PlayerChar();
  
      // Functions
      void SetPos(int x);
      bool DoFiring();
      void Fire(int x, int y);
      void Move();
      void Draw();
      void HandleInput(Uint8* keystates);
  
      // Size and position
      SDL_Rect box;
  };
  
  
  #endif
  </code></pre>