Archived

This topic is now archived and is closed to further replies.

narlymon

#define and vc++ is messin' with my head

Recommended Posts

Okay, I''m screwing around with VC++ 6.0, trying to get a decent feel for it. This compiles fine: if(intZoomLevel == 1){ pCmdUI ->SetCheck(1); }else{ pCmdUI ->SetCheck(0); } However, if I do this: #define ZOOM_LOCAL 1; ... if(intZoomLevel == ZOOM_LOCAL){ pCmdUI ->SetCheck(1); }else{ pCmdUI ->SetCheck(0); } I get all these error messages: error C2143: syntax error : missing '')'' before '';'' error C2059: syntax error : '')'' warning C4390: '';'' : empty controlled statement found; is this the intent? error C2181: illegal else without matching if WTF?!?!? It doesn''t recognize the if anymore? Why not? Any and all help would be appreciated.

Share this post


Link to post
Share on other sites
Yep, the thing is that anything with a # in front of it is not actually C code -- it''s a preprocesor directive that tells the compiler to do certain things. So a ; is not neccasary since the preproccesor doesn''t use it.

--TheGoop

Share this post


Link to post
Share on other sites
merlin9x9, that actually isn't true in C++. Well, at least it shouldn't be true

Constants are 'filled in' at compile time (since they don't change), and therefore won't take up any resources.

Actually, using the (free) Borland C++ 5.5 compiler using a simple test program and examining the assembler source generated, it can be seen that the compiler uses the values of consts directly instead of allocating space in memory to hold the value.

Erik

Edited by - Erik Post on 4/9/00 3:31:07 PM

Share this post


Link to post
Share on other sites
Oh, cool. I''d figured that was true but I''d read somewhere that they were implemented simply as variables except the compiler just made sure there weren''t any attempts to change it. Oh, well.

Share this post


Link to post
Share on other sites
Well, it''s legal to take the address of a const variable. i.e.
const int two = 2;
const int * two_ref = &two;
is valid code. In such cases the compiler is required to emit an entry in the data segment for the variable two. So consts variables will take up "additional" space in such an instance. Additional is in quotes, because, after all, you can''t do same thing with #define preprocessor directives.

Share this post


Link to post
Share on other sites