no global variables?

Started by
35 comments, last by nicho_tedja 21 years, 7 months ago
quote:Original post by d000hg
But this approach ties all the objects to a certain instance of the ''parent'' class - that doesn''t seem very object orientated. What if the object you store a link to is deleted, or replaced? Now you also have to put more extra safety code in. I just reckon a few globals is a lot easier.

It''s no more dangerous than a global that is allocated with malloc. And the ''particular instance'' could be allocated on the stack anyway. So there is no safety difference here. As for ''tying all objects'' to a certain instance, you have that anyway with the global. The benefit of the approach where you pass in the parameter is that one single extra line of code now lets you have 2 sets of objects, rather than adding another global and having to duplicate all the code that references it directly.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
Advertisement
quote:Original post by Kylotan
The benefit of the approach where you pass in the parameter is that one single extra line of code now lets you have 2 sets of objects, rather than adding another global and having to duplicate all the code that references it directly.

When would you want to have two sets of a CGame class? By definition, there should almost always be only one instantiation.

Cédric
quote:Original post by cedricl
When would you want to have two sets of a CGame class? By definition, there should almost always be only one instantiation.
For example I am working on top of a server framework, which automatically makes new server instances as required. So there will be serveral separate instances even of the main class I've written.

But using or not using globals is a matter of preference in most simple cases, like game clients. I just like dealing with objects and their direct references to each other. Global objects with state simply don't suit my idea of good OO programming (stateless objects, like a math class, should be global though). Globals also reduce the reusability of classes, since it's not clearly known which classes rely on these globals, and how they use them.

[edited by - civguy on August 18, 2002 8:37:33 AM]
quote:Original post by GayleSaver
Purism is a dangerous pursuit.


Couldn''t have said it better myself.

Things that you may want to multi-instance, or reuse elsewhere should be completely isolated from any globals or static things you may have.

But if neither concept makes any sense given the context, (like a "Game" class. When are you going to run more than one game simultaneously?) then go wild, make a singleton. Use a namespace, and stuff oh-so-dirty globals in it. Make stuffy people angry. :D
"There is only one everything"
The problem is, people say "oh, I''ll never need more than one of those!"... and then later on, they find out that they do. Like a ''Screen'' object... you think you''ll only need one and then someone asks for multimonitor support, and although the idea sounds cool, it involves rewriting a hell of a lot of code. Oops. One example where you might want more than one Game instance is where you want to run an automated playtest that pits computer players against each other. But creating lots of Game instances you could have several games running with only 1 set of overhead for sounds/textures/scripts and so on.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
[offtopic]

quote:Original post by d000hg
If you want a better OO language look at Java - the main is inside a class. In fact everything is a class!

Java may well be a better OO language than anything else out there, but the fact that main is in a class or that everything is a class do not constitute good arguments. I''ve never found a use for "My name is Oluseyi".Length(), for example (the value is known at code time; why bother with the function call? Might as well have had intrinsic types), and the fact that main must be made public and static point to a subtle workaround (in effect, main acts just like in C++, exists independently of any object instance and cannot be overloaded/concurrently executed in multiple).

I''m not bothering to dispute the point, just the arguments.
[/offtopic]
quote:Original post by nicho_tedja
I''m wondering how do you make a big program (games) without using global variables at all. I heard you can do this with classes, but I still don''t get it how.

The common solution would appear to be to use manager objects (which may or may not be singletons) with well-defined inter-relationships for passing data/events/etc which are declared within a non-global scope.

No rule is absolute and no solution is perfect. There will be times when you can think of no other solution than to use a global variable. In such cases, do so but comment generously such that, should a solution arise in the future, whoever is responsible for maintaining the code has a good idea how to update it without breaking dependent portions.

This topic is closed to new replies.

Advertisement