View Single Post
Onag

JCF Member

Joined: Feb 2002

Posts: 165

Onag is doing well so far

Oct 21, 2003, 04:55 PM
Onag is offline
Reply With Quote
Quote:
You don't want to use real global variables.
I'm actually using what are called static classes to provide variables in a global scope. It works similar to your method, but slightly different. A static member/method is one that exists outside of any instance of a class. In other words, if a file knows about a class, it can have access to static members without even creating an instance. For example, given the following files:

-------- globals.h --------

   class FakeGlobals
   {
      HWND HWindow;
      static void ShowWindow();
   };

---------------------------



------- globals.cpp -------

   #include "globals.h"

   HWND FakeGlobals::HWindow = NULL;

   void FakeGlobals::ShowWindow()
   {
      // ...
   }

---------------------------


A file would just have to #include globals.h to have access to the members/methods. For example:

-------- file1.cpp --------

   #include "globals.h"

   if(FakeGlobals::HWindow != NULL)
   {
      FakeGlobals::ShowWindow();
   }

---------------------------


This seems to work very well so far. There may be a hidden performance hit that I'm unaware of, but unless that proves true, I think I'm going to stick with this method. It makes for very clean code, and avoids the need for using extern.

-Nag