For example, I saw source code like the following. Can we use #define
in a function? How does it work? (more information: this code is what I copied from
Sure this is possible. The #define
is processed by the preprocessor before the compiler does anything. It is a simple text replacement. The preprocessor doesn't even know if the line of code is inside or outside a function, class or whatever.
By the way, it is generally considered bad style to define preprocessor macros in C++. Most of the things they are used for can be better achieved with templates.