473,382 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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/
Jul 19 '05 #1
2 3997
"bb snowman" <si**@freenet.de> wrote...
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
Jul 19 '05 #2
bb snowman <si**@freenet.de> wrote in message news:<op**************@news.freenet.de>...
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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
20
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
2
by: Abdessamad Belangour | last post by:
Hi all, and thanks for previous answers (especially Nicholas Paldino) An assembly is composed of a set of modules. The module class has a method for reading global methods GetMethods(). My...
2
by: Jason Shohet | last post by:
Lets say i have a numeric value drom a dropdown that is set on 1 page, but that the rest of my application needs afterwards. I'm trying to think of other ways besides a session / application...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
10
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.