Jump to content
  • Advertisement
Sign in to follow this  
EvilProgrammer

globals and statics

This topic is 5057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If you have a class (I'm talking C++ here) and you know you're only ever going to have one instance of it, and you need that instance to be global, I think the most conventional approach would be to create an instance of the class as a global variable. However, another possibility has occured to me: simply declaring all the class's members as static. This sounds weird to me, but interesting because this way you wouldn't ever even have to make an instance of the class. The only drawback I can think of is you would for sure only be able to have one instance of the class, and you couldn't reuse it in another program in which you needed two or more instances of the class. Still, this approach seems weird to me, and it seems kind of unconventional and not the way veteran C++ programmers would do it. If this is true, I want to know, specifically, why. Can anyone help me here?

Share this post


Link to post
Share on other sites
Advertisement
It's my understanding that it's not that you can't instantiate a class with static members.

You can have non-static members in the class, but the static members of the class can't use the non-static members, nor can you use the non-static members when the class isn't instantiated.

Share this post


Link to post
Share on other sites
Ah OK.


As for the draw back you mentioned, that would be called a 'singleton', where you only allow one instance of the class in question to reside in memory. With this kind of class, you control the ability of instantiation by making the constructor(s) protected/private.

Just having static members will not prevent you from instantiating the class multiple times, or prevent you from using the static members at the same time the class is instantiated somewhere else.


I don't know if it's a convention in the buisness to use techniques like this, but I don't see why it wouldn't be...

Share this post


Link to post
Share on other sites
The singleton pattern and the everything-static pattern (if it can be considered a pattern) are not the same. Singletons allow for precisely controlled lifetimes, as well as polymorphism.

The everything-static pattern is perfectly fine for things that are actually THINGS, as opposed to subsystems. For the latter, you may want to consider using a namespace instead of static elements.

Share this post


Link to post
Share on other sites
I don't particularly like the everything-static approach. It's a little harder to take something that's everything-static and then suddenly turn it into non-static, at the point where you realize you need polymorphism. It's also not clear what the threading promises are, and statics don't play flexibly with thread-local storage on many platforms.

Last, with statics, you still have to show all your internal members to the world, even if they're private. This means that if you need another private member, you still have to re-compile all files using your class, because you touched the header. Badness!

If you want "all statics," just use a namespace, and put the "members" hidden as static variables in the implementation (C++) file. This isolates users from the implementation details of your class, which is good.

If you use singletons, consider declaring only a singleton interface (abstract base class) and a singleton factory function, and actually define the concrete class in the .cpp file; again, this will isolate users from internal implementation changes. Putting members in public headers is just... bad, in my opinion.

Share this post


Link to post
Share on other sites
Ok, most of what I've read on Singleton's say you must make the constructor private/protected. However, in this example the constructor is public. Is it necessary that the constructor be private/protected or what makes this example different?

EDIT: Nvm, I think I understand. This singleton has you instantiate the class somewhere in the program, while others do it through the GetSingleton() function, but return a pointer if it already exists.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!