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

constants in header files


A common C++ trend is to use

const int foo = 10;

where some C programmers would have used

#define FOO 10

in order to avoid preprocessor overheads.

I wonder whether it is considered ok to have the former statement
appear in header files. When making accessible a nonconst variable
in a header file I would use the extern keyword and define it in a
corresponding source file. Does this principle apply to const variables
as well or is it ok to place such definitions in source files?

Thanks,

JG

Sep 15 '06 #1
3 10280

John Goche wrote:
A common C++ trend is to use

const int foo = 10;

where some C programmers would have used

#define FOO 10

in order to avoid preprocessor overheads.

I wonder whether it is considered ok to have the former statement
appear in header files. When making accessible a nonconst variable
in a header file I would use the extern keyword and define it in a
corresponding source file. Does this principle apply to const variables
as well or is it ok to place such definitions in source files?

Thanks,

JG
Yes. The extern principle apply also here. If you would have put the
definition itself in the header file, you will have an instance of
'foo' in each obj the included this header file.

As for the define Vs. const issue - There are many opinions of the pros
and cons of both. In general, I personally prefer the const method,
because there is a type check when using the const var. In some cases
it is more comfortable to use define...
As far as I know, in restpect of the foot print (the binary size) or
run time efficiency there is a very little difference, if at all.

Sep 15 '06 #2
gMorphus wrote:
>
As for the define Vs. const issue - There are many opinions of the pros
and cons of both. In general, I personally prefer the const method,
because there is a type check when using the const var.
There is also a type check when using a macro:

#define VALUE 1
#define FVALUE 1.0f

The type of VALUE is int and the type of FVALUE is float.

The place where you don't get type checking is function-style macros,
but that's not under discussion here.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 15 '06 #3
John Goche posted:
const int foo = 10;

This is equivalent to:

static const int foo = 10;

(The default changes from "extern" to "static" when dealing with const
global objects.)

It is both a declaration and a definition.

I wonder whether it is considered ok to have the former statement
appear in header files.

Yes, it's perfectly OK.

When making accessible a nonconst variable
in a header file I would use the extern keyword and define it in a
corresponding source file. Does this principle apply to const variables
as well or is it ok to place such definitions in source files?

Treat them the same, whether they're const or non-const. Just remember that
the default for global non-const objects is "extern", and that the default
for global const objects is "static".

--

Frederick Gotham
Sep 15 '06 #4

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

Similar topics

34
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
2
by: ason | last post by:
Does anybody know where to find all these API Constants like for example LVCFMT_BITMAP_ON_RIGHT = 4094; // = 0x1000 Are these Constants in any *.h files???? thanks ..::AsOn
7
by: JohnR | last post by:
Couldn't find it in MSDN. Does anybody have a listing of the message constants that would be received in my application.addmessagefilter routine in VB.net? They are referred to all over MSDN but...
12
by: pamelafluente | last post by:
Hi guys, what are the best place on the web where I can find the list of all thh constants like: WM_QUERYENDSESSION As Integer = &H11 (possibly with some explanations) .... etc ... to be...
27
by: Matt | last post by:
Does the language have header files containing definitions of numbers such as pi and e? I believe some implementations used to have those, but I can't find them in Stroustrup. Thanks.
8
by: Brian C | last post by:
Hello all, I have a quick question coming from a long time C programmer, trying to get back into C++. What I want to do is something like (C version): int OpenLog(char *Filename) { FILE...
2
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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
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...

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.