7.12 MACROS IN C


7.12 MACROS IN C++

Macros are ubiquitous in C, but used much less often in C++ programs. Macros are created by the preprocessor directive #define. Here is an example from C:

      #define ARRAY_SIZE 1000 

This definition would cause the C pre-processor to replace every occurrence of the name ARRAY_SIZE to be replaced by the number 1000. And if someday there was a need to change ARRAY_SIZE, one would need to edit only one line in the program, the line where the macro ARRAY_SIZE is defined.

Notwithstanding the obvious usefulness of macros for creating program constants, they tend to reduce the performance of C++ software tools such as debuggers, profilers, cross-reference tools, and so on, and their use is discouraged by Stroustrup [54]. In C++, a constant such as ARRAY_SIZE would be created by using the const modifier in a declaration, as in

       const int ARRAY_SIZE = 1000; 




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net