Global Variables and Object Orientation.

Started by
1 comment, last by Xest 22 years, 2 months ago
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.
Advertisement
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()
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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 ]

This topic is closed to new replies.

Advertisement