Global Declarations in Single File
Introduction
Here is a technique that I often use to place global variable declarations into a single global header file:
Source Code
Hopefully, the code is self-explanatory...
// Globals.h
#include "MyClass.h"
#ifdef INIT_GLOBALS
#define DECLARE_TYPE
#define IV(expression) =expression // Variables
#define IC(expression) (expression) // Classes
#undef INIT_GLOBALS
#else
#define DECLARE_TYPE extern
#define IV(expression)
#define IC(expression)
#endif
// Example
dclarations
DECLARE_TYPE int Global_Int IV(=100);
DECLARE_TYPE long Global_Long IV(=100);
DECLARE_TYPE MyClass Global_MyClass IC((100,200));
It's important to define INIT_GLOBALS in one file before it includes the Globals.h header file like this:
#define INIT_GLOBALS
#include "Globals.h"
// FYI: Example of My class, but its declaration is not
really important
// MyClass.h
class MyClass {
public:
MyClass(int AnInt, int AnotherInt) { m_MyInt=AnInt;
m_AnotherInt=AnotherInt; };
private:
int m_MyInt;
int m_AnotherInt;
};