What’s Next 787
21
Q&A ....................................................................................................................
Q If C++ offers better alternatives than the preprocessor, why is this option still
available?
A First, C++ is backward-compatible with C, and all significant parts of C must be
supported in C++. Second, some uses of the preprocessor are still used frequently
in C++, such as inclusion guards.
Q Why use macro functions when I can use a regular function?
A Macro functions are expanded inline and are used as a substitute for repeatedly
typing the same commands with minor variations. Again, however, templates usu-
ally offer a better alternative.
Q How do I know when to use a macro versus an inline function?
A Use inline functions whenever possible. Although macros offer character substitu-
tion, stringizing, and concatenation, they are not type-safe and can make code that
is more difficult to maintain.
Q What is the alternative to using the preprocessor to print interim values dur-
ing debugging?
A The best alternative is to use watchstatements within a debugger. For information
on watchstatements, consult your compiler or debugger documentation.
Q How do I decide when to use an assert()and when to throw an exception?
A If the situation you’re testing can be true without your having committed a pro-
gramming error, use an exception. If the only reason for this situation to ever be
true is a bug in your program, use an assert().
Q When would I use bit structures rather than simply using integers?
A When the size of the object is crucial. If you are working with limited memory or
with communications software, you might find that the savings offered by these
structures is essential to the success of your product.
Q Can I assign a pointer to a bit field?
A No. Memory addresses usually point to the beginning of a byte. A bit field might
be in the middle of a byte.
Q Why do style wars generate so much emotion?
A Programmers become very attached to their habits. If you are used to the following
indentation:
if (SomeCondition){
// statements
} // closing brace