global structs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bb snowman

    global structs

    Hi there,

    can anyone tell me how I can create a struture that is defined globally for
    several modules? I would like to do a configuration-struct that can be
    accessed from anywhere in my program. Something like this:

    conf.h - contains a struct conf{color blue, red};

    main.cpp - contains a function that uses conf.blue
    second.cpp - contains a function that uses conf.red

    Since I include the conf.h file traditionally I have to load the conf-
    struct in every .cpp-file, right? With what trick do I have to load the
    conf-struct only once (e.g. in main.cpp) and use it then in all my cpp-
    files?

    thx for reply
    chris

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
  • Victor Bazarov

    #2
    Re: global structs

    "bb snowman" <site@freenet.d e> wrote...[color=blue]
    > can anyone tell me how I can create a struture that is defined globally[/color]
    for[color=blue]
    > several modules?[/color]

    Usually, a header contains a declaration that has 'extern' in it.
    Then every module that _uses_ that global symbol needs to include
    that header. A separate module is chosen (or created) to hold the
    _definition_ of the symbol (and the definition should not contain
    the 'extern'). That separate module with the definition should
    include the header with the declaration as well.
    [color=blue]
    > I would like to do a configuration-struct that can be
    > accessed from anywhere in my program. Something like this:
    >
    > conf.h - contains a struct conf{color blue, red};
    >
    > main.cpp - contains a function that uses conf.blue
    > second.cpp - contains a function that uses conf.red
    >
    > Since I include the conf.h file traditionally I have to load the conf-
    > struct in every .cpp-file, right?[/color]

    No, usually, if it's a global structure, it will be constructed
    once before 'main' function is executed. You could "load the
    conf-struct" during its construction.
    [color=blue]
    > With what trick do I have to load the
    > conf-struct only once (e.g. in main.cpp) and use it then in all my cpp-
    > files?[/color]

    Use either a "static loader object" trick, or simply load it at
    the very beginning of the 'main' function.

    Victor


    Comment

    • Ryan

      #3
      Re: global structs

      bb snowman <site@freenet.d e> wrote in message news:<oprrq34qy 42iz45v@news.fr eenet.de>...[color=blue]
      > Hi there,
      >
      > can anyone tell me how I can create a struture that is defined globally for
      > several modules? I would like to do a configuration-struct that can be
      > accessed from anywhere in my program. Something like this:
      >
      > conf.h - contains a struct conf{color blue, red};
      >
      > main.cpp - contains a function that uses conf.blue
      > second.cpp - contains a function that uses conf.red
      >
      > Since I include the conf.h file traditionally I have to load the conf-
      > struct in every .cpp-file, right? With what trick do I have to load the
      > conf-struct only once (e.g. in main.cpp) and use it then in all my cpp-
      > files?
      >
      > thx for reply
      > chris[/color]

      I'll add an (untested) example to the already accurate explanation:

      glob.h
      ------
      struct conf { int i; };
      extern struct conf g_conf;

      glob.cpp
      --------
      #include "glob.h"
      struct conf g_conf;

      one.cpp
      -------
      #include "glob.h"
      void func1() { g_conf.i = 7; }

      two.cpp
      -------
      #include "glob.h"
      #include <iostream>
      void func2() { std::cout << g_conf.i; }


      If you're feeling comfortable with this, then try searching on
      "Singleton" and consider that implementation.

      Ryan

      Comment

      Working...