Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constants in header files

Reply
Thread Tools

constants in header files

 
 
John Goche
Guest
Posts: n/a
 
      09-15-2006

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

 
Reply With Quote
 
 
 
 
gMorphus
Guest
Posts: n/a
 
      09-15-2006

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.

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      09-15-2006
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.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-15-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
Re: physical constants header file rossum C++ 2 10-19-2004 08:57 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
Getting constants from *.h header files KKramsch Perl Misc 3 07-20-2004 12:35 AM



Advertisments