#define FOO //
FOO x++;
commenting with define
Hi, everyone, I started teaching myself c++ a couple weeks ago. Today I had a question I figured I might as well try posting here.
I was under the impression that when you use #define, it blindly replaces all occurrences before compiling beings, so I did
It compiled fine, but for some reason, the statements executed, even though they were supposed to be commented out. What did I do wrong? I hope this isn't a stupid question.
Comments are stripped before #defines are processed.
I hope you weren't planning to do that for real, though....
I hope you weren't planning to do that for real, though....
Quote:Original post by Codeka
I hope you weren't planning to do that for real, though....
It was the best way I could think of for enabling and disabling many statements easily
Quote:Original post by Storyyeller
It was the best way I could think of for enabling and disabling many statements easily
Then you'll want to learn about the other preprocessor directives that are for exactly that purpose.
Quote:Original post by StoryyellerQuote:Original post by Codeka
I hope you weren't planning to do that for real, though....
It was the best way I could think of for enabling and disabling many statements easily
Why not just use // in front or the line? It's less characters than FOO anyway.
Or for a longer statement, just use /* at the start and */ at the end of what you want to comment out.
Quote:Original post by ChadwellBecause it wouldn't allow him to conditionally disable a large number of nonconsecutive lines.
Why not just use // in front or the line? It's less characters than FOO anyway.
BTW, Storyyeller, there's a hackish and ugly way of doing this:
#define FOO if(0)
But yeah, #ifndef is your friend here.
Quote:Original post by Codeka
Comments are stripped before #defines are processed.
Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.
Quote:Original post by scrattQuote:Original post by Codeka
Comments are stripped before #defines are processed.
Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.
lol, if you comment the defines, then they will get stripped thus will not be compiled.
pass me some of those beers, tnx. ;)
Quote:Original post by owlQuote:Original post by scrattQuote:Original post by Codeka
Comments are stripped before #defines are processed.
Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.
lol, if you comment the defines, then they will get stripped thus will not be compiled.
pass me some of those beers, tnx. ;)
lol
I guess i read it the wrong way around.
i.e. Misinterpreting comments are stripped, as meaning // is stripped, before #define is processed, thus exposing #define.
Which it obviously does not.
My bad. :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement