Variables in global data space

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Otto Wyss

    Variables in global data space

    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.sourcef orge.net/" for ideas how to design your app.
  • John Harrison

    #2
    Re: Variables in global data space


    "Otto Wyss" <wyo@users.sour ceforge.net> wrote in message
    news:1g0q781.m4 i6fn7vuqvqN%wyo @users.sourcefo rge.net...[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


    Comment

    • Otto Wyss

      #3
      Re: Variables in global data space

      John Harrison <john_andronicu s@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.1 04&content-type=text/vnd.viewcvs-markup".

      Thanks, O. Wyss

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

      Comment

      Working...