I need to make a large (100's of source files) project into a library, removing dozens of global variables by putting them all into a class object.
The problem is the thousand or so functions that now need to be members of this class
so they have access to the object variables.
Otehr than adding MyClass::
to every single function definition in the source files, is there a way to cheat and indicate that all the functions in a particular source file should be part of the MyClass
scope?
Add all the globals to a namespace
.
// MyGlobals.h
namespace MyGlobals
{
extern int g_i;
extern double g_d;
extern A g_A;
}
Whatever files you want to access, do:
using namespace MyGlobals;
inside the header file. In this (using namespace
) way you can indicate that all the variables should be accessible without using scope resolution ::
for that file. (i.e. you can simply access g_i
instead of MyGlobals::g_i
inside that file).
Also note that, you have to define all the global variables inside a .cpp
file:
// MyGlobals.cpp
#include "MyGlobals.h"
int MyGlobals::g_i;
double MyGlobals::g_d;
A MyGlobals::g_A;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With