C++ Macros
Hello everybody, I am hoping that somebody can help me learn how to make macros in C++(like #define...). I''m not new to C++, it''s just that every programming class i''ve taken and every book i''ve read did not cover the topic. Maybe you can tell me about a good tutorial or book on the subject. Thank you.
a macro goes like this
#define MACRONAME(variable) expression
something simple, but useless would be this macro to multiply something by 2
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
Edited by - ncsu121978 on August 2, 2000 6:49:01 PM
Edited by - ncsu121978 on August 2, 2000 6:49:46 PM
#define MACRONAME(variable) expression
something simple, but useless would be this macro to multiply something by 2
#define MULTIPLY(x) x*2now to call in in your program you do thisint answer;answer = MULTIPLY(2);now answer will be 4you can even have 2 arguments to your macro#define MULTIPLY(a, b) a*bint answer;answer = MULTIPLY(2, 2);now answer is equal to 4
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
Edited by - ncsu121978 on August 2, 2000 6:49:01 PM
Edited by - ncsu121978 on August 2, 2000 6:49:46 PM
Thanks for the help. Could someone explain this macro to me?
define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))
I don''t understand what it all does(like <<), and what the final product of it is. Please help.
define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))
I don''t understand what it all does(like <<), and what the final product of it is. Please help.
#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \ ((WORD) (g) << 8)) | \ (((DWORD) (BYTE) (b)) << 16)))
Write a line like so...
DWORD color = RGB(255, 254, 253);
I assume BYTE is 1 byte integer, WORD is 2 byte integer, and DWORD is 4 byte integer.
Take first 255 casts it to a BYTE.
Result1 = 0xFF
Take the 254, cast it to a WORD, then shift it 8 bits to the left.
Result2 = 0xFE00
Bitwise Or these together.
Result3 = 0xFEFF
Take 253 cast to a BYTE, cast that to a DWORD (not sure why the intermediary step unless its to chop the input to a byte, but then why not do it on g). Take the DWORD and shift it 16 bits to the left.
Result4 = 0x00FD0000
Bitwise Or that with the result of the previous bitwise or. Result5 = 0x00FDFEFF
You now have the DWORD for a color very close to white.
Mike Roberts
aka milo
mlbobs@telocity.com
When you make a macro, always put parenthesis around your parameters to avoid potential operator prescedence during preprocessor expansion. Some might even argue putting parenthesis around the entire macro body, too, to be extra safe.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
C++ macro is a contradiction in terms - C++ inherited macros from C, but one of the aims of C++ is to make the C preprocessor redundant. so, instead of #define macros, use inlined functions, which are infinately better: they are type safe and are exactly as fast as macros (ie NO function call overhead at all.
we''re still stuck with #include and #ifndef header guards for now though (although, i hear that vc7 has an include directive feature: a sort of smart #include - YAY!)
we''re still stuck with #include and #ifndef header guards for now though (although, i hear that vc7 has an include directive feature: a sort of smart #include - YAY!)
quote:
so, instead of #define macros, use inlined functions, which are infinately better
Good advice, but for things like min & max, you''ll prob want to use a template function.
Macros are bad! They are an invention of Satan himself, and should be avoided. I totally agree with POINT. Inline all the way. Do away with old C type-unsafe macros and start anew with inline functions!
Lucas
Lucas
-=[ Lucas ]=-
August 06, 2000 07:36 AM
However in some cases your compiler will not inline for you so you will be forced to use macros unless your compiler supports forceinline
T.J
T.J
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement