Advertisement Jump to content


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


Global Variables and Object Orientation.

This topic is 6180 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
If you still want to use globals, you can stick them in a namespace.

namespace render
CComPtr spDevice;

Or make classes that have methods to acquire the resource;
IDirect3DDevice8* const device()

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!