Variables in global data space

O

Otto Wyss

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
 
J

John Harrison

Otto Wyss said:
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

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
 
O

Otto Wyss

John Harrison said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top