Thread: Best way to store a global constants file?

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    Best way to store a global constants file?

    I'm familiar with VB.NET projects in storing global constants, but what is the best way to store them n a C++ project? For example, several files in the project uses the same constants. I use to throw it all into a header, then just include that header wherever needed. Is that the most suggested way if you have several constants?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Probably. You might want to use "#ifndef" style header guards.
    Code:
    #ifndef THISPARTICULARHEADER
    #define THISPARTICULARHEADER 1
    [...everything here...]
    #endif
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't see why you would need global constants that would apply across the entire application.

    Normally if I use a constant then I include the enumeration or const in the header for the interface. In this way the constants and enums are only exposed to those who are implementing the interface which should be the only objects that actually need to use the constants. As well the interface, consts, and enumerations are inside of a namespace. You should not need a file that only contains globals for the rest of the application.
    Last edited by VirtualAce; 04-10-2010 at 01:12 PM.

  4. #4
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Quote Originally Posted by MK27 View Post
    Probably. You might want to use "#ifndef" style header guards.
    Code:
    #ifndef THISPARTICULARHEADER
    #define THISPARTICULARHEADER 1
    [...everything here...]
    #endif
    Thanks, MK.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That is a simple include guard. It does not address your possible design issues.

    #pragma once works on MSVS 2005+

  6. #6
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Quote Originally Posted by MK27 View Post
    Probably. You might want to use "#ifndef" style header guards.
    Code:
    #ifndef THISPARTICULARHEADER
    #define THISPARTICULARHEADER 1
    [...everything here...]
    #endif
    Normally I use this format:

    #ifndef _MYHEADER_H_
    #define _MYHEADER_H_
    // code here
    #endif

    Does this do the same thing as the above if I don't put "1" after it?

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The value is negotiable, but you shouldn't use a leading underscore.

    Soma

  8. #8
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Does this do the same thing as the above if I don't put "1" after it?
    Yes, macros can be defined but have no value.

    EDIT: got beat to it. I ought to make a habit of refreshing before I reply.
    Consider this post signed

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dxfoo View Post
    Does this do the same thing as the above if I don't put "1" after it?
    Nah, that's a paranoid habit of mine I think (it will still be defined).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    I've ran into issues where just doing #ifndef blocks in the header doesn't quite cut it.

    Rather, in the header do extern const <datatype> variablename, and in a second cpp globals file do all your constants.


    [edit]Ah now I remember why I had to do that. The globals where function pointers, and the functions where defined elsewhere. I had to move the function pointers into the file with the functions they where pointing to and declare the extern in the global file.. It was somewhat a mess (It was all for the purpose of being able to redefine functions... I don't know if it was terribly useful, but it was what it was). A regular ifndef block should work well enough, unless you want to do something fancy.[/edit]
    Last edited by Cogman; 04-11-2010 at 09:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM