473,657 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global scope variable in .cpp file?

If I put (define) const variables at the top of a .cpp file but do not
declare them in the .h file, are they only visible within that .cpp file?

e.g.

const int a[]={1,2,3,4};

in a cpp file will be a 'global' with file scope? (no flames for
oxymoron, please), available for use inside my functions which are
defined in the same file (but which are also declared in the .h)?

On trying it, this appears to be true, but then:
In this case, what does the 'static' keyword lend to the definition?
Schildts' book (C++ The complete reference) tells me that "Applying the
specifier static to a global variable instructs the compiler to create a
global variable that is known only to the file in which you declared it."

cheers

shaun
Jan 23 '06 #1
6 8337
* shaun:
If I put (define) const variables at the top of a .cpp file but do not
declare them in the .h file,
The files declarations are placed in do not matter. What matters is
only the sequence the compiler encounters the declarations in. The C++
standard does not rely on a notion of "file", and indeed your source
code does not need to reside in a file; e.g., if you're using the g++
compiler, try the command

g++ -x c++ -

and then just type in some C++ source code (how you indicate end of text
depends on the system, e.g. Unix [Ctrl D] or Windows [Ctrl Z] Return).

are they only visible within that .cpp file?

e.g.

const int a[]={1,2,3,4};
If there is no previous declaration of 'a' then 'a' has internal
linkage, which means it's not accessible by name outside the compilation
unit (in practical terms, it's invisible to the linker).

in a cpp file will be a 'global' with file scope? (no flames for
oxymoron, please), available for use inside my functions which are
defined in the same file (but which are also declared in the .h)?
Yes, in the sense explained above.

On trying it, this appears to be true, but then:
In this case, what does the 'static' keyword lend to the definition?
Schildts' book (C++ The complete reference) tells me that "Applying the
specifier static to a global variable instructs the compiler to create a
global variable that is known only to the file in which you declared it."


Look up what the [alt.comp.lang.l earn.c-c++] FAQ says about Schildt...

The C++ standard is not concerned with files (see example above).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 23 '06 #2

shaun wrote:
If I put (define) const variables at the top of a .cpp file but do not
declare them in the .h file, are they only visible within that .cpp file?

e.g.

const int a[]={1,2,3,4};

in a cpp file will be a 'global' with file scope? (no flames for
oxymoron, please), available for use inside my functions which are
defined in the same file (but which are also declared in the .h)?


That is a C thing only I believe. In C you can still access a variable
by name in another object file even if you don't have that name
declaired in the current object file. Using extern just gets rid of
warnings. Using static on a "global" in C creates a file scope
variable. In C++ I believe that any variable you don't have declaired
in your scope just plain isn't in your scope (where in C if there is no
static it is a *global*) so the static keyword serves no purpose in
such declarations in C++.

I could be wrong...I tend to refrain from using globals.

Jan 23 '06 #3

ro**********@gm ail.com wrote:
shaun wrote:
If I put (define) const variables at the top of a .cpp file but do not
declare them in the .h file, are they only visible within that .cpp file?

e.g.

const int a[]={1,2,3,4};

in a cpp file will be a 'global' with file scope? (no flames for
oxymoron, please), available for use inside my functions which are
defined in the same file (but which are also declared in the .h)?


That is a C thing only I believe. In C you can still access a variable
by name in another object file even if you don't have that name
declaired in the current object file. Using extern just gets rid of
warnings. Using static on a "global" in C creates a file scope
variable. In C++ I believe that any variable you don't have declaired
in your scope just plain isn't in your scope (where in C if there is no
static it is a *global*) so the static keyword serves no purpose in
such declarations in C++.

I could be wrong...I tend to refrain from using globals.


And I am: http://david.tribble.com/text/cdiffs...static-linkage

Jan 23 '06 #4
>If I put (define) const variables at the top of a .cpp file but do not
declare them in the .h file, are they only visible within that .cpp file?
e.g.
const int a[]={1,2,3,4};
In this case during compilation stage yes it is visible only to that
..cpp file. but during linking stage it is accessible by all the files.
Though in linking stage all the files are combined to form a single
executive.....

In this case, what does the 'static' keyword lend to the definition?
Schildts' book (C++ The complete reference) tells me that "Applying the
specifier static to a global variable instructs the compiler to create a
global variable that is known only to the file in which you declared it."


But making a variable static makes it visible only to that cpp only. It
cannot be accessed in other files by any means.

If you just declare a global variable e.g.
int value = 10;
you cannot create a global variable with the same name in any of your
files. This shows that in linking stage a global variable is visible to
all the files. but not a static variable.

Jan 23 '06 #5

jo***********@g mail.com wrote:
If I put (define) const variables at the top of a .cpp file but do not
declare them in the .h file, are they only visible within that .cpp file?
e.g.
const int a[]={1,2,3,4};


In this case during compilation stage yes it is visible only to that
.cpp file. but during linking stage it is accessible by all the files.
Though in linking stage all the files are combined to form a single
executive.....


Not if (as in the OP's case) the name is explicitly declared const.
File-scope const objects have internal linkage.
In this case, what does the 'static' keyword lend to the definition?
Schildts' book (C++ The complete reference) tells me that "Applying the
specifier static to a global variable instructs the compiler to create a
global variable that is known only to the file in which you declared it."


But making a variable static makes it visible only to that cpp only. It
cannot be accessed in other files by any means.


For a const object, static in this case adds nothing. Well, it adds to
the confusion of different meanings for the static keyword. Which is
why this use of static is deprecated.

Gavin Deane

Jan 23 '06 #6
* jo***********@g mail.com:
If I put (define) const variables at the top of a .cpp file but do not
declare them in the .h file, are they only visible within that .cpp file?
e.g.
const int a[]={1,2,3,4};
In this case during compilation stage yes it is visible only to that
.cpp file. but during linking stage it is accessible by all the files.
Though in linking stage all the files are combined to form a single
executive.....


Sorry, that's incorrect: in C++ const variables default to internal
linkage.
[snip] But making a variable static makes it visible only to that cpp only. It
cannot be accessed in other files by any means.
First, C++ does not care about the physical organization of the program
text in files.

Second, a static variable can be accessed anywhere via a pointer or
reference.

It's only for access by name that there is a restriction, namely to the
translation unit (note: that's not the same as a file) it appears in.

If you just declare a global variable e.g.
int value = 10;
you cannot create a global variable with the same name in any of your
files.
Sorry, that's incorrect: avoiding such possible name clashes is what C++
namespaces are for.

This shows that in linking stage a global variable is visible to
all the files. but not a static variable.


When you write "global variable" here you probably mean a variable with
external linkage. Be aware that others use the same term, "global
variable", with other meanings. In particular, many find it perfectly
reasonable to call an internal linkage variable of namespace scope a
"global variable".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 23 '06 #7

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

Similar topics

8
2534
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
5
3489
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file scope. If you have a variable whose scope is file scope in another translation unit, you have to provide a local declaration to access that variable from the other translation unit. Also, I don't see "global variable" used once in the standard....
3
1657
by: Zhigang Cui | last post by:
Hi, When I saw 'global static', the first response was there was no such term. But I realized it's an idiom later. When we learn C language, when we study C standard, when we study compiler, we use 'linkages of identifiers', 'storage-class specifier'. One of my friends said maybe we could use 'local static', :-) Who could tell me when/where 'global static' came from? Are there some other similar idioms?
2
2366
by: Estella | last post by:
Hello, I wrote a function called eat_path() to split a string into components e.g. /a/b/c ==> namePtr = a,namePtr = a, namePtr = c // Global variable char *namePtr = {0}; int n; /*number of components*/ The function is like this: void eat_path(char *pathname)
7
3129
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
44
3609
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
7
2571
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope) global variables outside of the files they were defined it, and created some files that just hold global variables definitions (just variables, without any function definition). So, depending on the purpose/category of variables, they're defined...
1
29343
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
112
5425
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.