< previous page page_352 next page >

Page 352
The actual name of the defined symbol (ANIMAL_HPP) is not important, although it is customary to use the filename in all uppercase with the dot (.) changed to an underscore. This is purely convention, however.
It never hurts to use inclusion guards. Often they will save you hours of debugging time.

Defining on the Command Line
Almost all C++ compilers will let you #define values either from the command line or from the integrated development environment (and usually both). Therefore, you can leave out lines 1 and 2 from Listing 21.1 and define DemoVersion and BetaTestVersion from the command line for some compilations and not for others.
It is common to put in special debugging code surrounded by #ifdef DEBUG and #endif. This allows all the debugging code to be easily removed from the source code when you compile the final version: just don't define the term DEBUG.
Undefining
If you have a name defined and you'd like to turn it off from within your code, you can use #undef. This works as the antidote to #define.
Conditional Compilation
By combining #define or command-line definitions with #ifdef, #else, and #ifndef, you can write one program that compiles different code depending on what is already #defined. This can be used to create one set of source code to compile on two different platforms, such as DOS and Windows.
Another common use of this technique is to conditionally compile in some code based on whether DEBUG has been defined, as you'll see in a few moments.
Macro Functions
The #define directive can also be used to create macro functions. A macro function is a symbol created using #define and taking an argument, much like a function does. The preprocessor will substitute the substitution string for whatever argument it is given. For example, you can define the macro TWICE as
#define TWICE (x) ( (x) * 2 )

 
< previous page page_352 next page >

If you like this book, buy it!