Jump to content

  • Log In with Google      Sign In   
  • Create Account


how to use static variables properly in c++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 GameGeazer   Members   -  Reputation: 739

Like
0Likes
Like

Posted 24 February 2013 - 09:51 AM

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, 24 February 2013 - 09:53 AM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9540

Like
8Likes
Like

Posted 24 February 2013 - 10:00 AM

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.

#3 GameGeazer   Members   -  Reputation: 739

Like
0Likes
Like

Posted 24 February 2013 - 11:01 AM

Thank you very much.



#4 EWClay   Members   -  Reputation: 659

Like
2Likes
Like

Posted 24 February 2013 - 12:13 PM

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.

#5 Servant of the Lord   Crossbones+   -  Reputation: 18487

Like
0Likes
Like

Posted 24 February 2013 - 06:09 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS