global structs

B

bb snowman

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
 
V

Victor Bazarov

bb snowman said:
can anyone tell me how I can create a struture that is defined globally for
several modules?

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.
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?

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.
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?

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

Victor
 
R

Ryan

bb snowman said:
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

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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top