Terminology - global

B

bob

Stroustrup 3rd edition, 4.9.4, 2nd paragraph:
"A name is called global if it is declared outside any function, class
(Chapter 10), or namespace. The scope of a global name extends from the
point of declaration to the end of the file in which its declaration
occurs."

Is x in the following global?

// Start of a file
static const int x = 0;
class MyClass
{
//etc.

I know such usage is deprecated (ref: B.2.3 in the above).
 
V

Victor Bazarov

bob said:
Stroustrup 3rd edition, 4.9.4, 2nd paragraph:
"A name is called global if it is declared outside any function, class
(Chapter 10), or namespace. The scope of a global name extends from the
point of declaration to the end of the file in which its declaration
occurs."

Is x in the following global?

// Start of a file
static const int x = 0;
class MyClass
{
//etc.

I know such usage is deprecated (ref: B.2.3 in the above).

Yes, it is global in the sense used in the quote you presented.

I am more used to a small correction (or addition) to that definition.
A name defined outside of any function, class, or namespace, _and_ has
external linkage. That makes it really global, i.e. visible from other
translation units.

However, the quote you gave is in the section that talks about scope,
about name hiding, and about the ways to access global names using the
scope resolution operator :):), so linkage is not involved.

V
 
B

bob

Thanks Victor.
This was provoked by a code review at my place of employment.
The code snippet was:

// Local
static const int x = 0;
class { etc.

I objected to the "Local" comment (and the fact that such usage is
deprecated).
Believe it or not, this lead to about 5 email exchanges.
The problem is semantics as you pointed out:
There is "global" and "really global".
The term "global" (ala Stroustrup) is overloaded in the sense that it
can refer to either meaning. I think many/most programmers use the
"really global" interpretation and thus the "Local" comment in the code
snippet.
Bob.
 
A

Alf P. Steinbach

* bob:
Thanks Victor.
This was provoked by a code review at my place of employment.
The code snippet was:

// Local
static const int x = 0;
class { etc.

I objected to the "Local" comment (and the fact that such usage is
deprecated).
Believe it or not, this lead to about 5 email exchanges.
The problem is semantics as you pointed out:
There is "global" and "really global".
The term "global" (ala Stroustrup) is overloaded in the sense that it
can refer to either meaning. I think many/most programmers use the
"really global" interpretation and thus the "Local" comment in the code
snippet.

One alternative is to use a word such as "private". Of course that's
overloaded too. Any way, the comment above is like this one:

a = b + c; // Compute the sum of b and c, and assign that to a.

If someone doesn't know what "static" means, then that someone has
no business maintaining the code.

So I agree, the comment was a bad 'un (I also agree with Victor about
the usual interpretation of "global", and there are more).
 
?

=?iso-8859-1?q?Daniel_Lidstr=F6m?=

There is "global" and "really global".
The term "global" (ala Stroustrup) is overloaded in the sense that it
can refer to either meaning. I think many/most programmers use the
"really global" interpretation and thus the "Local" comment in the code
snippet.

How can the variable be made "really" global?
 
V

Victor Bazarov

Daniel said:
How can the variable be made "really" global?

If you look at the original post and my reply, the variable in question
was declared 'static' which meant it had internal linkage. It is also
const, which gives it even more internal linkage (if that's possible).
To make it "really global" in *my* meaning of "global", it had to be
declared 'extern', which along with the initialiser would give it external
linkage while defining it in this module:

extern const int x = 0;

Now, 'x' is visible from other translation units. If in another module
you declare it

extern const int x;

then the symbol will be resolved during linking.

V
 
H

Howard

Victor Bazarov said:
If you look at the original post and my reply, the variable in question
was declared 'static' which meant it had internal linkage. It is also
const, which gives it even more internal linkage (if that's possible).
To make it "really global" in *my* meaning of "global", it had to be
declared 'extern', which along with the initialiser would give it external
linkage while defining it in this module:

extern const int x = 0;

Now, 'x' is visible from other translation units. If in another module
you declare it

extern const int x;

then the symbol will be resolved during linking.

V

Why do you need the "extern" where it's defined... because it's a const?
For variables that are not const, the extern specifier is only needed in the
"other" modules, correct?

-Howard
 
V

Victor Bazarov

Howard said:
[..]
Why do you need the "extern" where it's defined... because it's a const?
For variables that are not const, the extern specifier is only needed in the
"other" modules, correct?

Yes, both correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top