< previous page page_357 next page >

Page 357
and this would be turned into the declaration of the AnimalList class. There are some problems with this approach, all of which are discussed in detail in Hour 23, when templates are discussed.
Predefined Macros
Many compilers predefine a number of useful macros, including __DATE__, __TIME__, __LINE__, and __FILE__. Each of these names is surrounded by two underscore characters to reduce the likelihood that the names will conflict with names you've used in your program.
When the precompiler sees one of these macros, it makes the appropriate substitutes. For __DATE__, the current date is substituted. For __TIME__, the current time is substituted. __LINE__ and __FILE__ are replaced with the source code line number and filename, respectively. You should note that this substitution is made when the source is precompiled, not when the program is run. If you asked the program to print __DATE__, you will not get the current date; you will get the date the program was compiled. These defined macros are very useful in debugging.
assert()
Many compilers offer an assert() macro. The assert() macro returns true if its parameter evaluates true, and takes some kind of action if it evaluates false. Many compilers will abort the program on an assert() that fails, others will throw an exception. (See Hour 20, Special Classes and Functions.)
One powerful feature of the assert() macro is that the preprocessor collapses it into no code at all if DEBUG is not defined. It is a great help during development, and when the final product ships there is no performance penalty nor an increase in the size of the executable version of the program.
Rather than depending on the compiler-provided assert(), you are free to write your own assert() macro. Listing 21.3 provides a simple assert() macro and shows its use.
LISTING 21.3 A SIMPLEassert() MACRO

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:     // Listing 21.3 ASSERTS
2:     #define DEBUG
3:     #include <iostream.h>
4:
5:     #ifndef DEBUG
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_357 next page >

If you like this book, buy it!