how to use static variables properly in c++

Started by
3 comments, last by Servant of the Lord 11 years, 2 months ago

Hey, I've been getting an error "Undefined reference to Game:: window." I was wondering how I would go about declaring a static variable, initializing it, and accessing it properly. Thanks for your time.

cc file:


sf::RenderWindow* const Game::getWindow(void){
        return Game::window;
    }

h file:


public:
 static sf::RenderWindow* const getWindow(void);
private:
static sf::RenderWindow* window;
Advertisement
In one source file at namespace scope (not inside a function or class definition) you need to put sf::RenderWindow * Game::window;. The static declaration in a class definition still needs a definition. It's sort of like an extern declaration for a global.

Thank you very much.

What you have there is a global variable. It may be declared static and accessed through a function but that's all it is.

I'm guessing that the window is not actually shared between multiple Game objects but you just want easy access to it. There's probably a better way to do it, e.g. passing the window as a function parameter.

As EWClay mentioned, it'd be better to pass it through to functions individually.

In SFML, in most situations, you might actually want to take a non-const sf::RenderTarget by reference, like this:


void MyGameObject::draw(sf::RenderTarget &renderTarget)
{
    //...
}

Since sf::RenderWindow inherits from sf::RenderTarget, and since the function takes the sf::RenderTarget by reference, you can pass a sf::RenderWindow to the function and it'll still work like normal. But it also adds the additional benefit that you can pass other types of sf::RenderTargets (like off-screen buffers) to the functions as well.

Another benefit to passing things directly to functions, instead of making sf::RenderWindow static, is that not every function in different classes need to know about it, and it's good style to limit what code can access what.

Also, you might later decide to actually have more than one window (which SFML supports, iirc), and if you pass to functions instead of functions accessing it globally, you can use the same functions to draw to either window, just by passing the different windows as parameters.

A fourth reason to not make it static or global is that if it's static or global you might be tempted to use it in the constructor of some class or another, and then you might be tempted to make that class global as well. The problem is, the order of initialization of static/global variables is not defined, so it can lead to broken code that is confusing to debug - your class might be constructed first and try to use the RenderWindow that hasn't been created yet. sad.png

The only guarantee of global/static variables is that they'll all be initialized (in an unknown order) before you enter main() - but if they depend on each other, it may appear to work with one compiler and break with another.

This topic is closed to new replies.

Advertisement