user2589054
user2589054

Reputation: 45

C++ SFML member initializer

I'm really confused at the moment. It's been a while since I touched C++ and I want to get back into it again, so I'm trying to use SFML, and I'm using a guide along side it. The thing that gets me is this code.

Game::Game():mWindow(sf::VideoMode(640, 480), "SFML Application") {
}

It works perfectly, and I think I understand. It's initializing mWindow, and when mWindow is constructed, it creates the window. No problem. But when I put this code down.

Game::Game() {
    mWindow(sf::VideoMode(640, 480), "SFML Application");
}

It gives me the error Type 'sf::RenderWindow' does not provide a call operatorwhich I have no idea what that means, nor do I understand how those two blocks of code differ.

Upvotes: 0

Views: 400

Answers (3)

Aesthete
Aesthete

Reputation: 18848

The error it's giving you is because in your second example, this:

mWindow(sf::VideoMode(640, 480), "SFML Application");

Should be this:

mWindow = sf::VideoMode(640, 480), "SFML Application";

It's not going to work anyway, as sf:RenderWindow inherits from sf::NonCopyable.

The error is actually quite descriptive, 'sf::RenderWindow' does not provide a call operator. mWindow is of type sf::RenderWindow. You're trying to call it like a function, mWindow(), which it is not.

You should use the initializer list in your first example. This will ensure the the mWindow member is constructed once.

Upvotes: 1

pabdulin
pabdulin

Reputation: 35245

This is called Constructor Initialization Lists and it actually should be something like this in your second example:

Game::Game() {
    this.mWindow = new sf::RenderWindow(sf::VideoMode(640, 480), "SFML Application");
}

Upvotes: 0

nullpotent
nullpotent

Reputation: 9260

mWindow should be a class data member, not a function.

First block of code simply initializes it.

Upvotes: 2

Related Questions