Skip to main content
gamedev.net
🔒 Locked

Static class as global variable

Started by MV 3 replies 550+ views
Original Post
MV
MV
Is this a good idea ? For example for an object in charge of the textures or stuff ? I saw some previous threads on global variables using extern and gave up using them so I start using static and it works fine with objects with one occurence.
pinacolada
pinacolada
Yeah that''s a fine idea. Conceptually, a public static thing is a global, cause it is accessable anywhere and there is only one of it. It just has different syntax. And public statics certainly have cleaner code than globals, since you have to qualify them with the class name every time.

If you want a super-robust way to take care of objects that will only have one instance, you might be interested in the "singleton" design pattern.
Captian Goatse
Captian Goatse
no no no no no.

in before singleton

oh no.. beaten

Don't do it. Globals are bad and singleton is a global. If you have to use globals use namespace. No reason to complicate your code with singleton.

Singleton does not magically cure the problems with globals.

[edited by - captian goatse on January 28, 2004 2:39:36 PM]
Mayrel
Mayrel
Globals are fine for single-threaded applications. If the program is multithreaded you''ll need to bear in mind that all threads share the globals.

Putting globals inside classes is usually a good idea because it means you''re less likely to get name conflicts between globals.

Using singletons isn''t going to magically make your code any good. Singleton is related to the allocation policy of your objects, not the visibility policy.

CoV
psykr
psykr
Umm.. Captian Goatse, there is no problem with globals. Just like any other aspect of a programming language, globals have their place. Yes, there are problems that you have to consider when using them, but there are also problems to consider with any other method you have of getting around a problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.