Globals in MFC

MFC class framework makes possible to work easily with global variables and functions.
Let’s declare a static member variable or function in the CWinApp derived class. It is
accessible from all classes.


// MyApp.h
class CMyApp : public CWinApp

  static int g_nMyVariable;      // declaration
  static void g_MyFunction();    // declaration
// MyApp.cpp
int CMyApp::g_nMyVariable = NULL;     // definition & initialization
void CMyApp::g_MyFunction()        // definition

Now the variable and the function are accessible through the syntax CMyApp::g_nMyVariable or CMyApp::g_MyFunction from any other class…


More by Author

Must Read