constants in header files

Discussion in 'C++' started by John Goche, Sep 15, 2006.

  1. John Goche

    John Goche Guest

    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
     
    John Goche, Sep 15, 2006
    #1
    1. Advertising

  2. John Goche

    gMorphus Guest

    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.
     
    gMorphus, Sep 15, 2006
    #2
    1. Advertising

  3. John Goche

    Pete Becker Guest

    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.
     
    Pete Becker, Sep 15, 2006
    #3
  4. 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
     
    Frederick Gotham, Sep 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rossum
    Replies:
    2
    Views:
    435
    rossum
    Oct 19, 2004
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    637
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,359
    Walter Roberson
    May 1, 2006
  4. mlt
    Replies:
    2
    Views:
    915
    Jean-Marc Bourguet
    Jan 31, 2009
  5. KKramsch

    Getting constants from *.h header files

    KKramsch, Jul 19, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    111
    KKramsch
    Jul 20, 2004
Loading...

Share This Page