| re: Variables in global data space
"Otto Wyss" <wyo@users.sourceforge.net> wrote in message
news:1g0q781.m4i6fn7vuqvqN%wyo@users.sourceforge.n et...[color=blue]
> I have a string variable g_appname which should be global accessable by
> any class who whishes. I have defined it as
>
> wxString *g_appname = NULL;
>
> and fills it later on. Now a friend asked why I didn't write
>
> wxString g_appname = "Name";
>
> I had to answer "I don't know", I just had the impression in global it
> should be a reference. Again when he asked why I didn't use a static
> variable in a class, I didn't know an answer beside not knowing which
> class it belongs to. So what's correct in which case?
>
> O. Wyss
>[/color]
There are no restrictions on what types a global variable can have.
wxString g_appname = "Name";
is fine. However there is the issue of which order global variables are
constructed in. Its perfectly possible to write code where the construction
of one global variable assumes that another global variable has been
constructed first. But except for the case where the two global variables
are in the same file it is not possible to say in which order two global
variables will be constructed.
Your code that uses a pointer does not suffer from this problem. It is
guaranteed to be NULL at the moment the program starts. The constructors of
other global variables can safely assume this to be true.
As for the static variable in a class argument. Its not possible to know
whether that a good idea without seeing the design of your code.
john |