Connecting Tech Pros Worldwide Forums | Help | Site Map

Variables in global data space

Otto Wyss
Guest
 
Posts: n/a
#1: Jul 19 '05
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

--
See "http://wxguide.sourceforge.net/" for ideas how to design your app.

John Harrison
Guest
 
Posts: n/a
#2: Jul 19 '05

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


Otto Wyss
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Variables in global data space


John Harrison <john_andronicus@hotmail.com> wrote:
[color=blue][color=green]
> > wxString *g_appname = NULL;
> >
> > and fills it later on. Now a friend asked why I didn't write
> >
> > wxString g_appname = "Name";
> >[/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.
>[/color]
In other words, it doesn't matter much unless there are dependences
between global variables.

You may look at the full source here:
"http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/wxguide/wxGuide/editor/s
rc/app.cpp?rev=1.104&content-type=text/vnd.viewcvs-markup".

Thanks, O. Wyss

--
See "http://wxguide.sourceforge.net/" for ideas how to design your app.
Closed Thread