473,387 Members | 1,431 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,387 software developers and data experts.

const int FOO vs #define FOO


When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?

I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.

Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...
Feb 20 '06 #1
9 5102

C. J. Clegg wrote:
When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?
No. It's visible in the definition scope. I.e. if you define it in the
true-branch of
an if-statement, it's invisible in the else-branch. A #define will be
visible until
the #undef, or EOF.
I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.
Same scope? Besides, you must give them the same type.
Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...


IIRC, yes, unless defined extern. But since I only use extern in the
corresponding
header, this is never a problem. Using extern in an unrelated .h or
..cpp is a hack,
IMO.

HTH,
Michiel Salters

Feb 20 '06 #2
On 20 Feb 2006 07:57:22 -0800, Mi*************@tomtom.com wrote:
C. J. Clegg wrote:
When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?


No. It's visible in the definition scope. I.e. if you define it in the
true-branch of
an if-statement, it's invisible in the else-branch. A #define will be
visible until
the #undef, or EOF.


I define it in global space, i.e. outside of any function or class.

In fact, for test, I have a file foo.cpp that has the single line

const int FOO = 0;

.... then I say "extern const int FOO;" at the top of other files, but
the other files can't see it.

So the question is, does it then become visible only to the file,
similar to "static int FOO"?
Feb 20 '06 #3
C. J. Clegg wrote:
On 20 Feb 2006 07:57:22 -0800, Mi*************@tomtom.com wrote:
C. J. Clegg wrote:
When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?
No. It's visible in the definition scope. I.e. if you define it in the
true-branch of
an if-statement, it's invisible in the else-branch. A #define will be
visible until
the #undef, or EOF.


I define it in global space, i.e. outside of any function or class.

In fact, for test, I have a file foo.cpp that has the single line

const int FOO = 0;

... then I say "extern const int FOO;" at the top of other files, but
the other files can't see it.


Instead, put that in a header file "foo.h" and #include that header in all
of those files as well as foo.cpp.
So the question is, does it then become visible only to the file,
similar to "static int FOO"?


AFAIK, constants on namespace scope have internal linkage by default.

Feb 20 '06 #4
C. J. Clegg wrote:
When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?

I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.

Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...


Yes it does. You can declare FOO as having external linkage by
prefixing the definition with 'extern', like this:

extern const int FOO = 0;

/ALiX

Feb 20 '06 #5
On Mon, 20 Feb 2006 18:56:16 +0100, Rolf Magnus <ra******@t-online.de>
wrote:
C. J. Clegg wrote:

In fact, for test, I have a file foo.cpp that has the single line

const int FOO = 0;

... then I say "extern const int FOO;" at the top of other files, but
the other files can't see it.


Instead, put that in a header file "foo.h" and #include that header in all
of those files as well as foo.cpp.


Good evening, Rolf.

Ah, but "const int FOO = 0" allocates memory, specifically a block of
size int, and you're not supposed to put stuff in .h files that
allocate memory.

Anyway, if I say "const int FOO = 0" in a .h file and then #include
that .h file in two other files, don't I end up with two FOOs, and in
fact won't I get yelled at by the linker for duplicate symbols?
Feb 21 '06 #6
On 20 Feb 2006 11:03:05 -0800, "ALiX" <al********@hotmail.com> wrote:
C. J. Clegg wrote:

Does "const" make it static (file scope)? Certainly seems to...


Yes it does. You can declare FOO as having external linkage by
prefixing the definition with 'extern', like this:

extern const int FOO = 0;


Thanks, I will try that.
Feb 21 '06 #7
C. J. Clegg wrote:
Ah, but "const int FOO = 0" allocates memory, specifically a block of
size int, and you're not supposed to put stuff in .h files that
allocate memory.

Anyway, if I say "const int FOO = 0" in a .h file and then #include
that .h file in two other files, don't I end up with two FOOs, and in
fact won't I get yelled at by the linker for duplicate symbols?


You would in C, but not in C++. A const int in C is an int variable that
may not be changed. A const int in C++ is a compile-time name for the
constant number given in the definition, and allocates no storage.

--
Ron House ho***@usq.edu.au
http://www.sci.usq.edu.au/staff/house
Feb 21 '06 #8
On Mon, 20 Feb 2006 10:27:26 -0500, C. J. Clegg
<re****************@nospam.no> wrote in comp.lang.c++:

When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?

I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.

Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...


You have just bumped into one of the "silent" changes from C in C++.
"const" on an object defined at file scope in C++ also gives the
object internal linkage, unlike in C.

The solution is to use:

extern const int FOO = 0;

....to restore the external linkage when you want it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 21 '06 #9
On Tue, 21 Feb 2006 13:45:45 +1000, Ron House <ho***@usq.edu.au> wrote
in comp.lang.c++:
C. J. Clegg wrote:
Ah, but "const int FOO = 0" allocates memory, specifically a block of
size int, and you're not supposed to put stuff in .h files that
allocate memory.

Anyway, if I say "const int FOO = 0" in a .h file and then #include
that .h file in two other files, don't I end up with two FOOs, and in
fact won't I get yelled at by the linker for duplicate symbols?


You would in C, but not in C++. A const int in C is an int variable that
may not be changed. A const int in C++ is a compile-time name for the
constant number given in the definition, and allocates no storage.


....that is, unless its address is taken, of course.

Given:

extern int func1(const int &);
extern int func2(const int *);

const int FOO = 0;

int main()
{
return func1(FOO) + func2(&FOO);
}

....you can be fairly confident that FOO will be allocated storage,
especially if the called functions are in a separate translation unit.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 21 '06 #10

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

Similar topics

8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: Rayer | last post by:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in...
4
by: yancheng.cheok | last post by:
Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For...
3
by: Louis Caron | last post by:
I am facing the following problem: - I have to build a const array containing functions pointers - the indexes for this array are generated automatically by a tool (not my property) How do I...
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...
2
by: Jason | last post by:
What is the difference between 'const' and 'define()' ? When would I prefer using 'const' over 'define', or vice versa? It seems if i do: const secondsInMinute = 60; define("secondsInMinute",...
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.