Sign in to follow this  
GameGeazer

how to use static variables properly in c++

Recommended Posts

GameGeazer    1090

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;
Edited by GameGeazer

Share this post


Link to post
Share on other sites
EWClay    659
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this