Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
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?
 

crees!

macrumors 68020
Jun 14, 2003
2,018
245
MD/VA/DC
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.
 

ChrisA

macrumors G5
Jan 5, 2006
12,917
2,169
Redondo Beach, California
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?


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];
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
#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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.