Global Variables and Object Orientation.
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.
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()
namespace render
{
CComPtr spDevice;
};
Or make classes that have methods to acquire the resource;
IDirect3DDevice8* const device()
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 ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement