0

I was reading a book and it said
"Mac names cannot be overloaded and the macro preprossor cannot handle recursive calls:

#define PRINT (a,b)cout<<(a)<<(b)
#define PRINT (a,b,c) cout<<(a)<<(b)<<(c) /*trouble?:redefines,does not overload*/

Can someone pls explain possibliy with examples?

Edited by Valiantangel: correction

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Valiantangel
1

You can't actually code them as you posted them because the second line will produce a "redefination" error message. Once a macro has been defined you have to undefine it before it can be re-defined.

#define PRINT (a,b)cout<<(a)<<(b)
#undef PRINT
#define PRINT (a,b,c) cout<<(a)<<(b)<<(c) /*trouble?:redefines,does not overload*/

You don't have to define macros at the beginning of a program -- they can be defined, undefined, and re-defined anywhere you want to do it. The compiler will use the most recent definition when compiling the remainder of the program from the point that the macro is defined to the end of the program file.

Macros are evil beasts. One reason is because of what I just said above, that macros can be redefined anywhere in the program making it difficult for a programmer to figure out why something went wrong.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.