SFML community forums

Help => General => Topic started by: Pilly on March 22, 2018, 02:26:20 am

Title: Help understanding why a constructor is written like this in SFML
Post by: Pilly on March 22, 2018, 02:26:20 am
I don't know if this is a C++ question or an SFML question but what exactly is happening here in this constructor. I've never seen a constructor written like this before and I can't find any other examples online.

Rather than using opening braces, it starts with a semi-colon, and why comma's after texture, player, font, etc? If Texture is a member variable why is there a ()?

Game::Game()
   : Window(sf::VideoMode(Resolution.x, Resolution.y), "SFML Application", sf::Style::Close)
   , Texture()
   , Player()
   , Font()
   , isMovingUp(false)
   , isMovingDown(false)
   , isMovingRight(false)
   , isMovingLeft(false) {
   if (!Texture.loadFromFile("Graphics/character.png")) {
      // loading error
   }
   Player.setTexture(Texture);
   Player.setPosition(100.f, 100.f);
   }

Why can't I get the constructor to work like this?

Game::Game() {
   Window(VideoMode(Resolution.x, Resolution.y), "SFML Application", sf::Style::Close);
   isMovingUp = false;
   isMovingDown = false;
   isMovingRight = false;
   isMovingLeft = false;

   if (!Texture.loadFromFile("Graphics/character.png")) {
         // loading error
      }
      Player.setTexture(Texture);
      Player.setPosition(100.f, 100.f);
   }
}
Title: Re: Help understanding why a constructor is written like this in SFML
Post by: AlejandroCoria on March 22, 2018, 03:01:41 am
That is called initializer list (http://en.cppreference.com/w/cpp/language/initializer_list).