Archived

This topic is now archived and is closed to further replies.

Global Variables and Object Orientation.

This topic is 5776 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

Hi, I''m a C programmer who''s having to go the C++ route, when coding an OpenGL application I normally have a global structure g_Globals or some such containing my device and rendering context, flag to define if the program is active etc. to make it easy to access them from wherever I am in the program. In C++, what are your recommended ways of handling global variables, is it a good idea to create a class, say CMain or something to hold said variables, and even say, wndproc, initialization functions in ?, if so how do I declare a class globally. If this is seen as bad practice in C++, how should I go about storing these variables ? Thanks, Xest.

Share this post


Link to post
Share on other sites
You have to think about the program in a different way. So-called ''globals'' are rarely truly global anyway - they just tend to have a wide visibility. But how often do you use your screen pointer in the sound system? Instead of thinking about functions acting upon data, think about how you split the system up into subsystems, and about how subsystems can contain several objects that interact. These objects become your classes, and contain most of the data. What would normally be a global in a language like C often ends up as either one of these objects, or a member of a subsystem class.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites