🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #474

Re: Sharing a variable through[out] my project

#474
From: George Swan
Date: Tue, 03 Feb 1998 00:00
54 lines
2865 bytes
In article <6b4r80$7c@bgtnsc03.worldnet.att.net>,
Julius Lancer <postmaster@0.com> wrote:
>In article <MPG.f3f021a49d3df279896a3@news.edmonton.telusplanet.net>, adamst@telusplanet.net (Trenton D. Adams) wrote:

>>My question now is this.  How the heck do I make a variable that will 
>>keep it's value through my whole program?

>Two choices:
>1. Declare the variable "static" within a function.
>2. Declare the variable  outside of a function. To access the variable form 
>another file declare the variable again with "extern".

Third choice:

Create a header file, called something like "globaldf.h",
that looks something like this:

#ifdef	DEFINE_GLOBALS
#	define	EXTERN
#	define	INIT( c )	c
#	define	INIT2( a,b )	a,b
#else
#	define	EXTERN	extern
#	define	INIT( c )
#	define	INIT2( a,b )
#endif

In one of your source files, define the manifest constant
DEFINE_GLOBALS.  Then #include all your header files.  #include
"globaldf.h" first.  Declare the variables you want to share in
your header files.  Make your declarations look like this:

EXTERN	char  *	ProgramName;	/* runtime invocation name for error reports */
EXTERN	char  *	OutputFileName	INIT( = "/dev/null" );
EXTERN	XYPair	Origin		INIT2( = { 10, 20 } );

The advantage of this approach is that you only declare the variables
in a single place.  In Julius's second choice you are declaring
variables in two or more places, and this can become a dangerous
nuisance if you have to change the variables types.

I didn't invent this technique.  I saw it in a newsgroup article a
number of years ago.  But I have used it quite a bit, and never had
a problem with it, except that the preprocessor that came with
my BC++ 3.1 barfs when you use a c++ style comment when calling
a macro, like this:

EXTERN  XYPair  Example     INIT2( =            // the c++ style
    {                                           // comments screw up
         0, /* x location */                    // the preprocessor.
        10  /* y location */                    // It's as if it loses
    }       /* c style comments are OK */       // track of the line-ends.
);
Technical Details
Message-ID: <6b7q7p$928@globalserve.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <MPG.f3f021a49d3df279896a3@news.edmonton.telusplanet.net> <6b4r80$7c@bgtnsc03.worldnet.att.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f8d38,cc5639ecc55682f1
X-Google-Attributes: gidf8d38,public
X-Google-Thread: 109fba,cc5639ecc55682f1
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,cc5639ecc55682f1
X-Google-Attributes: gid114592,public
From: gswan@globalserve.net (George Swan)
Subject: Re: Sharing a variable through[out] my project
Date: 1998/02/03
Message-ID: <6b7q7p$928@globalserve.net>
X-Deja-AN: 321751607
References: <MPG.f3f021a49d3df279896a3@news.edmonton.telusplanet.net> <6b4r80$7c@bgtnsc03.worldnet.att.net>
Organization: Globalserve Communications Inc.
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++