List of constants to share in my project

Discussion in 'Mac Programming' started by Eraserhead, May 16, 2008.

  1. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #1
    I'd like to stop using numbers for variables which should be constants. How do you create a file which contains a list of constants for use across my application?
     
  2. crees! macrumors 68000

    crees!

    Joined:
    Jun 14, 2003
    Location:
    MD/VA/DC
    #2
    Speaking in generalities you could just create a class that holds these values. When you need to reference them elsewhere just import the class and access them as you would any public variable in a another class.
     
  3. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #3

    The most common way is to use #define and define them all as macros. BY convention all upper case is used. Put these in a dot H file and #include it where needed.

    #define DAYSINWEEK 5
    #define FINGERPERHAND 10
    #define TWOPI (2.0*pi)

    THese kinds of macros can be used anyplace. Like to define an array
    int days[DAYSINWEEK];
     
  4. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #4
    I'm assuming this is C or Obj-C?

    Put them in a header file where they can be #defines, const ints, or expanding on the suggestion above, static const members of some globally accessible class. If this is C++ or Obj-C++, you can also create a namespace to hold these constants.
     
  5. Eraserhead thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    Thanks for the help, I presume if I use #define the constants can't be changed without a recompile?

    This is for my D&D program so its Objective-C but most of the constants are C constants. I thought I should stop using ==2 and things like that when actually its a constant value thats required as that's bad code.
     
  6. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #6
    #define's can't be changed without a recompile, or more strictly speaking, another run of the preprocessor and a recompile (these are usually represented as one step, although they don't have to be).

    But if what you want to express really is a constant, compile-time is the only time it should be changed. Compared to #define's, declaring const objects has the advantage that it introduces a symbol into the symbol table, and so it's visible in the debugger. There is also the risk that your #define may silently redefine some other #define elsewhere in your code or that in an included header, which can result in some very obscure bugs.
    The drawback to const objects, besides the negligible space penalty, is that const doesn't really mean constant, but rather "read-only through that symbol". That is an object declared const can't be assigned to, but that doesn't stop you from playing tricks like taking a const object's address and manipulating the contents via a pointer dereference.
     

Share This Page