473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

global const variable being local to a file by default

I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that
const variables at global scope are local to a file by default.

What is the advantage of this rule ?

Suppose I have the following const variable defined in a.h

const int const_int = fn();

Suppose a.h is #included in a.cpp which also contains the following
function.

int fn()
{
static int counter;
return ++counter;
}

Assume a.cpp uses the variable 'const_int'. Suppose a.h is #included
in another file b.cpp which uses 'const_int'

We will get different values for the const int variable 'const_int'
when it is used in these two files. Am I correct ? This situation
arises because the compiler allows a non-const expression to be used
as the initializer for the const variable and yet it allows the const
variable to be available local to the files.

Why doesn't the compiler DISALLOW this ? ie If the compiler disallows
a const variable to be defined in multiple .cpp files, this will not
arise.

Please correct me wherever I have gone wrong.

Kindly explain.

Thanks
V.Subramanian
Nov 30 '07 #1
4 2709
On Nov 30, 2:36 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that
const variables at global scope are local to a file by default.

What is the advantage of this rule ?

Suppose I have the following const variable defined in a.h

const int const_int = fn();

Suppose a.h is #included in a.cpp which also contains the following
function.

int fn()
{
static int counter;
return ++counter;

}

Assume a.cpp uses the variable 'const_int'. Suppose a.h is #included
in another file b.cpp which uses 'const_int'

We will get different values for the const int variable 'const_int'
when it is used in these two files. Am I correct ? This situation
arises because the compiler allows a non-const expression to be used
as the initializer for the const variable and yet it allows the const
variable to be available local to the files.

Why doesn't the compiler DISALLOW this ? ie If the compiler disallows
a const variable to be defined in multiple .cpp files, this will not
arise.

Please correct me wherever I have gone wrong.

Kindly explain.

Thanks
V.Subramanian
You can't have two fn() defined in two different cpp files and still
link them in one executable files...
Nov 30 '07 #2
On Nov 30, 1:36 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that
const variables at global scope are local to a file by default.
What is the advantage of this rule ?
To confuse people, and frustrate programmers. It's obviously a
mistake, but for whatever reasons, it's there, and it can't be
changed at this late date.
Suppose I have the following const variable defined in a.h
const int const_int = fn();
Suppose a.h is #included in a.cpp which also contains the following
function.
int fn()
{
static int counter;
return ++counter;
}
Assume a.cpp uses the variable 'const_int'. Suppose a.h is #included
in another file b.cpp which uses 'const_int'
We will get different values for the const int variable 'const_int'
when it is used in these two files. Am I correct ?
You have two different variables, with two different values,
yes. If you only want one, then you have to declare:

extern int const const_int ;

in the header, and:

extern int const const_int = fn() ;

in one (and only one) source file.

Which, by the way, might explain why the rule is there. Here,
it's not an issue, because the initialization expression is not
const itself, but if you want to use the variable as a compile
time constant (integral constant expression), then the compiler
has to see the initializer. Which means that the initializer
must be present in the header, which means that the header must
contain a definition, not a declaration, which means that if it
doesn't have internal linkage, you have a violation of the one
definition rule.

There are other ways around this; the way static const is
handled in a class, for example. But they also cause some
confusion, and I don't think that the compiler technology in the
early days of C++ would have easily supported them.
This situation arises because the compiler allows a non-const
expression to be used as the initializer for the const
variable and yet it allows the const variable to be available
local to the files.
Why doesn't the compiler DISALLOW this ? ie If the compiler
disallows a const variable to be defined in multiple .cpp
files, this will not arise.
I think you mean "if the compiler allowed a const variable to be
defined in multiple source files". It does, in the case of
static members, but that's very much an exception. And I'm not
sure that the technology to support this was available in the
early days.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 30 '07 #3
* On Dec 1, 1:53 am, James Kanze <james.ka...@gmail.comwrote:
On Nov 30, 1:36 pm, "subramanian10...@yahoo.com, India"

<subramanian10...@yahoo.comwrote:
I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that
const variables at global scope are local to a file by default.
What is the advantage of this rule ?

To confuse people, and frustrate programmers. It's obviously a
mistake, but for whatever reasons, it's there, and it can't be
changed at this late date.
--
James Kanze (GABI Software)
As a help I am asking the following question:

If the above question(and also similar questions where some peculiar
situation is present in the standard itself) is asked in the
interview, what is your advise to me to answer the interviewer. In
real application I will not use the above situation.

Kindly clarify.

Thanks
V.Subramanian
Dec 1 '07 #4
On Dec 1, 8:01 am, "Alf P. Steinbach" <al...@start.nowrote:
* subramanian10...@yahoo.com, India:
* On Dec 1, 1:53 am, James Kanze <james.ka...@gmail.comwrote:
On Nov 30, 1:36 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that
const variables at global scope are local to a file by default.
What is the advantage of this rule ?
To confuse people, and frustrate programmers. It's obviously a
mistake, but for whatever reasons, it's there, and it can't be
changed at this late date.
As a help I am asking the following question:
If the above question(and also similar questions where some peculiar
situation is present in the standard itself) is asked in the
interview, what is your advise to me to answer the interviewer. In
real application I will not use the above situation.
You can answer that it's less to write for the most common case.
Actually, I'd answer that I don't want the job. Any place
asking questions that stupid isn't a place I want to work for.
(I don't think the amount you have to write was an actual
consideration when the rule was formulated, but it may have
been, and it's as good a (non-)reason as anything else.)

Of course, questions designed to determine whether the candidate
knows this particular rule, and more generally, whether he knows
the rules for determining linkage (which are far from trivial)
are fair game.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 2 '07 #5

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

Similar topics

3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
9
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
4
by: Bilgehan.Balban | last post by:
Hi, The following code: #include <stdio.h> // const int const_asize = 10; #define define_asize = 10; int array = {1,2,3,4,5,6,7,8,9,0};
6
by: shaun | last post by:
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...
5
by: akarui.tomodachi | last post by:
I have about twenty functions written in C. Each time I call a function, it logs the status of the function operation to a file. I use a central logging function which is called from each function...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
1
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...

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.