I l@ve RuBoard Previous Section Next Section

10.6 Summary

The C++ preprocessor is a very useful part of the C++ language. It has a completely different look and feel from C++. However, it must be treated apart from the main C++ compiler.

Problems in macro definitions often do not show up where the macro is defined, but result in errors much further down in the program. By following a few simple rules, you can decrease the chances of having problems:

  • Put parentheses around everything. In particular they should enclose #define constants and macro parameters.

  • When defining a macro with more than one statement, enclose the code in { }.

  • The preprocessor is not C++. Don't use = or ;.

    #define X = 5 // Illegal
    #define X 5;  // Illegal
    #define X = 5; // Very illegal
    #define X 5   // Correct

Finally, if you got this far, be glad that the worst is over.

    I l@ve RuBoard Previous Section Next Section