Ok, I normally post in the General Programming section, but for some reason, I can't figure out how to do this and I feel it's a pretty newbie question.
I have a node based XML parser, and I'm reading simulation data files and simulation parameter files. I know when and when not to use globals in C++, but in this case, several simulation parameters are read just inside the main(), but arent used until halfway through the simulation, and at a significantly lower level in the simulation than they were set. I have an implementation where they are passed through in constructors all the way down to use level, but this means that several classes have access to simulation data they have no business knowing (which is against the tenets of object oriented programming).
What I would like is a simple way of setting a few global constants, and labelling them const so they can't be changed by anyone accessing them. However, they must be set inside a function, since I can't explore xmlnodes and extract the data from outside executable code.
I imagine a header file "globals.h", that has something like:
extern const double SIMULATION_START_TIME_JD
extern const double SIMULATION_END_TIME_SECONDS
and then the main function defining:
// LOTS OF INCLUDES
#include "globals.h"
main(int argc, char *argv[ ], char *envp[ ])
{
string inputFileName = argv[1];
XMLNode mainXMLNode = XMLNode::openFileHelper(inputFileName.c_str() , "SCENARIO");
XMLNode simParametersXMLNode = mainXMLNode.getChildNode("simulation");
static double SIMULATION_START_TIME_JD = (atof(simParametersXMLNode.getAttribute("startJD")));
static double SIMULATION_END_TIME_SECONDS = (atof(simParametersXMLNode.getAttribute("simEnd")));
// LOTS OF STUFF HERE
}
then any time I wanted to use those variables, I just include the header globals.h and rock.
What's the best way to do this?
[Edited by - chaospilot on March 23, 2007 5:33:38 PM]