Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help understanding why a constructor is written like this in SFML  (Read 1449 times)

0 Members and 1 Guest are viewing this topic.

Pilly

  • Newbie
  • *
  • Posts: 2
    • View Profile
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);
   }
}
« Last Edit: March 22, 2018, 02:46:43 am by Pilly »

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Help understanding why a constructor is written like this in SFML
« Reply #1 on: March 22, 2018, 03:01:41 am »
That is called initializer list.

 

anything