[LC++]declaring and using manipulating global variables

Robert Wuest rwuest at wuest.org
Fri May 16 23:17:01 UTC 2003


I like to declare all my globals in a class globals g.

If you make everything public, you get access through g.variableName. 
You can easily add accessor/mutator functions if you prefer that style. 
  Handy if you need to add locking for threads or something like that as 
your code grows.

I use two files, globals.h and globals.cpp. globals.h contains the class 
def and is included anywhere you use any global variables.  globals.cpp 
has the constructor, where everything is initialized and it has the 
_one-and-only_ instance of g.

Here's an example of :

in globals.h:

class globals {
public:
	globals();
	int index;
};



in globals.cpp:

globals::globals()
{
	index = 0;
}

globals g;		// The ONE instance




in main.cpp:

#include "globals.h"
void printIndex()
{
	printf("global index is %d\n", g.index );
}

int incrementIndex()
{
	return ++g.index;
}

int main( int, char** )
{
	while( g.index<10 ) {
		incrementIndex();
		printIndex();
	}
}



devraj sanyal wrote:
> may be you are using a local glob_index in the change_glob_index()
> function.
> for clarity use g_<Variable Name>.
> also you may try using macros like
> INCGLOB ++glob_index, DECGLOB --glob_index.
> 
> There is no harm in using globals. It improves effciency etc.
> But its good practice to have wrapper functions/macros to access or 
> modify globals. The wrappers may be inlined to eliminate the calling
> overhead.
> 
> devraj
> 
> 
> 
> On Fri, 16 May 2003 Julien Patrick Claassen wrote :
> 
>> Hi!
>>   My unhappiness. I want to use a global variable for counting up an
>> array-index. But now all functions that deal with my array have to get 
>> it as
>> a parameter. That makes me unhappy, because most of them, don't even do
>> something with it directly (they may just pass it along for another
>> "subfunction" they call. Besides, I thought there was a way to declare a
>> global variable, that is known everywhere and can be changed 
>> everwhere. Being
>> changed meaning, changing it globally. I simply tried it with 
>> something like:
>>
>> #include <something>
>> int glob_index;
>> (functions here using glob_index);
>> int main()
>> {
>>   glob_index = 6;
>>   change_glob_index(); // being a function that manipulates glob_index
>>   return 0;
>> }
>>
>>   I used some simple output to see the value of glob_index and saw, 
>> that it is
>> only changed locally.
>>   Is there a - let's say - more elegant way of doing this? Or is my 
>> way the
>> usual?
>>   Kindest regards
>>         Julien
>>
>>
>> Julien Patrick Claassen
>> jclaassen at gmx.de
>> julien at c-lab.de
>> http://www.geocities.com/jjs_home
>>
>> SBS C-LAB
>> Fuerstenallee 11
>> 33102 Paderborn
>>
>> Phone: (+49) 5251 60 6060
>> Fax: (+49) 5251 60 6065
>>
>> www.c-lab.de
>> _______________________________________________
>> This is the Linux C++ Programming List
>> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
> 
> 
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
> 
> 




More information about the tuxCPProgramming mailing list