Topic: global variables


Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/07/18
Raw View
It is global for all other files in the project. If you put the keyword
'static' in front of your global variable it becomes "global" only in that
file.

Nanos Apostolos wrote:

> When a global variable is declared in one file, is it global for all the
> files in the project, or is it global only in that file?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Nanos Apostolos <rubbit@ccf.auth.gr>
Date: 1998/07/16
Raw View
When a global variable is declared in one file, is it global for all the
files in the project, or is it global only in that file?



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jimi Lee" <leesi@adam.kaist.ac.kr>
Date: 1998/07/16
Raw View
>When a global variable is declared in one file, is it global for all the
>files in the project, or is it global only in that file?

It's your choice....

If the variable is declared in Only one file, it's only global in tha file.
But, with "extern", if other files have a information of that variable,
it is global in these files.

Example)
;File A.h
..... <blah>
extern int Global_Variable;
..... <blah>

;File A.cpp
int Global_Variable;
int Local_Global_Variable;

<Blah...>

;File B.cpp
#include "A.h"

In this case, Global_Variable can be accessd in B.cpp (and of course in
A.cpp).
But Local_Global_Variable only in A.cpp. Can't in B.cpp




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/07/18
Raw View
Jimi Lee wrote:
>
> If the variable is declared in Only one file, it's only global in tha
> file. But, with "extern", if other files have a information of that
> variable, it is global in these files.
>
> Example)
> ;File A.h
> ..... <blah>
> extern int Global_Variable;
> ..... <blah>
>
> ;File A.cpp
> int Global_Variable;
> int Local_Global_Variable;
>
> <Blah...>
>
> ;File B.cpp
> #include "A.h"
>
> In this case, Global_Variable can be accessd in B.cpp (and of course
> in A.cpp).
> But Local_Global_Variable only in A.cpp. Can't in B.cpp

That's a bit misleading. Local_Global_Variable most definitely _is_
visible to B. It's just that B doesn't happen to contain a declaration
for it (just as it might not contain a declaration for strlen() or
errno).

Any variable declared at global scope is visible outside that module
unless it is declared static or const, or placed in an unnamed
namespace. If it is declared const, it can be made visible outside the
module by also declaring it extern. The different treatment of const
(compared to C) was added to C++ to encourage its use for manifest
constants, instead of relying on #define.

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]