Re: Use static variable across multiple files??
On Mar 21, 6:24 am, "Robert Crandal" <nob...@gmail.com> wrote:
But technically, just remove the "static" from your
definition, unless you have a top level "const", in which
case you also need to add "extern".
Really, I can remove the "static" definition and my global
variable will still "exist" for the duration of my program??
If the variable is defined at namespace scope. What other
lifetime could it conceivably have?
BTW, I know it's usually a bad idea to use global variables,
but my applications always seem to require a collection of
"state" variables that need to exist for the entire duration
of my GUI program.
The state of what? There are a few things that should exist for
the entire duration of a program, independently of what the
program does otherwise, but they aren't very frequent. (An
output stream for logging comes to mind, and in some cases,
configuration data, but that's about it.)
In your opinion, what's a good technique for managing a
collection of state variables that might need to be accessed
at any time by another C++ file module?? I usually just
create a small set of unique global variable names...if no
other variables share the same names I don't see what can go
If it's a collection, then the first thing to do is to put it
into a class or some other single object. Then, depending on
what it's used for, associate an instance of this object with
whatever is appropriate.