1. variable scope
- Posted by Nathan Green <lochlainn10 at hotmail.com> Mar 07, 2003
- 466 views
I have a question regarding variable scopes. Is it possible to declare a variable or constant that is truly global, across all includes? In reading the manuals, I understand the desire to keep scope limited to a routine, but I don't understand the why of limiting to a given include, or an include and the main program, but not a truly global variable or constant across all files. Am I misunderstanding the use of the include files? I want to be able to break my libraries into neat groups, but several of these libraries, distinctly different in my mind, require the same variable. I would rather not have to pass that variable constantly to functions in different files; it's set once and never changes. (A pointer to a bitmap used as a back buffer.) Any clarifications about the scope of variables or how I can get around this? Am I thinking wrongly about the use of include files? Thanks, Nate
2. Re: variable scope
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Mar 08, 2003
- 485 views
On Fri, 7 Mar 2003 21:57:23 +0000, Nathan Green <lochlainn10 at hotmail.com> wrote: >I have a question regarding variable scopes. Hello. > >Is it possible to declare a variable or constant that is truly global,=20 >across all includes? Let me clarify: --start of source: include a.e -- does not know what 'g' is global integer g -- here we define 'g'(!) include b.e -- can refer to 'g' >>include c.e -- can also refer to 'g' > >In reading the manuals, I understand the desire to keep scope limited to= =20 >a routine, but I don't understand the why of limiting to a given=20 >include,=20 example (file name is "x.e"): integer a include y.e -- this line coded in file "x.e" ::file "y.e" is: integer b -- rest of file "y.e" can see b include z.e -- this line coded in file "y.e" ::file "z.e" is: integer c -- visible to code in file "z.e" -- rest of code in file "z.e" cannot see a or b -- rest of code in file "y.e" cannot see a or c -- rest of code in file "x.e" cannot see b or c prefixing with "global" makes the variable visible to subsequent include files, and a "global" defined within an include file is visible to all lines in the file following the include statement. Most variables (90%+) are only relevant to the immediately following code; only a handful tend to be needed by all your code. You'll understand this alot more as you learn to program. >or an include and the main program, but not a truly global=20 >variable or constant across all files. > >Am I misunderstanding the use of the include files? I want to be able=20 >to break my libraries into neat groups, but several of these libraries,=20 >distinctly different in my mind, require the same variable. Yes, you are definitely misunderstanding this. Global variables can be referenced in include files, and in files included by include files. I'm not sure; you may be confused by the 'namespace' problem whereby a (global) variable x, defined *more than once* can (naturally) cause confusion. Defining a variable named x twice in the same file is always an error, however when it is defined is separate files this rule is relaxed - until you try to reference it externally to the file(s) in which it is defined.=20 The rule is relaxed because you may be using several 3rd party include files written by someone else, as long as it is obvious by context (namely within an included source file) there is no problem; however referencing it externally requires a namespace qualifier - and that is probably where your confusion lies.=20 >rather not have to pass that variable constantly to functions in=20 >different files; it's set once and never changes. (A pointer to a=20 >bitmap used as a back buffer.) > >Any clarifications about the scope of variables or how I can get around=20 >this? Am I thinking wrongly about the use of include files? > >Thanks, >Nate > > > >TOPICA - Start your own email discussion group. FREE! =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D