properties of "#define" in C++

Started by
6 comments, last by Kryodus 19 years, 2 months ago
Ive just recently started going through tutorials, looking at sample code etc. Now, in one of the sample codes that ive seen, they use #define, I assume to define a method. It could be SDL for all I know, since thats the only other package the .cpp file imports. I have never seen this syntax before, anyone know what it does? if it does just define a method, why use it over void <method name>(<parameter>)? heres the sample code: #define PUTPIXEL(x,y,c) ((unsigned int*)screen->pixels)[(x) + (y) * (screen->pitch / 4)] = (c); ive tried to search google, but ive been unsusscesful since # does not register on the search engine
Advertisement
click
Mike's resource might be helpful to you, but possibly a little over your head if you have never seen the syntax before. #define can be used in a couple of ways:
You can define an 'identifier' to represent a certain value, like this:
#include <iostream>#define X 5int main(){    cout << X << endl;}

The program will display 5 on the console

#define can also be used to define macros. A macro is a piece of code that has some of the functionality of a function :), but should not be confused with one:
#include <iostream>#define R(x) x*2int main(){    cout << R(9) << endl;}

The preceeding snippet will print 18 on the console. Be aware that macros can have bad side effects that are hard to detect, and should be used minimally, especially if you are coding in C++.

There are other ways that #define can be used, but I think that the example you showed was a macro. Hope that was helpful...
#define is used to replace text in your source code before it gets send off to the compiler.

For example, with:

#define R(x) x*2int main(){ cout << R(9) << endl;}


After the preprocessor gets through with it, all the compiler is going to see is this:

int main(){ cout << 9*2 << endl;}


Since it is just substituting text, unexpected things can happen. For example:

#define SIX    1 + 5#define NINE   8 + 1int main(){ cout << (SIX * NINE) << endl;}


This becomes:

int main(){ cout << (1 + 5 * 8 + 1) << endl;}


Since multiplication has a higher precedence than adding, this will print 42 instead of the expected 54.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Notes:

  • The preprocessor language (which #define is a part of) is not C++. It is used in C++ programs, but is not itself C++ code.

  • The preprocessor is not intelligent. It essentially performs copy/paste operations.

  • Because the preprocessor is a non-intelligent text replacement tool, it does not respect scopes and has no notion of type rules. If macro names or #defined constants conflict with extant variable names, this will not raise errors; the preprocessor will blindly replace them.



Personally, I think that the preprocessor is useful for conditional compilation (#ifdef and its compatriots), and it is of course necessary to #include header files, but should otherwise be avoided as much as possible.
Quote:#define R(x) x*2

that should be

#define R(x) ((x)*2)
Quote:Original post by Kryodus
Ive just recently started going through tutorials, looking at sample code etc.

Now, in one of the sample codes that ive seen, they use #define, I assume to define a method.

It could be SDL for all I know, since thats the only other package the .cpp file imports.

I have never seen this syntax before, anyone know what it does?
if it does just define a method, why use it over
void <method name>(<parameter>)?

heres the sample code:
#define PUTPIXEL(x,y,c) ((unsigned int*)screen->pixels)[(x) + (y) * (screen->pitch / 4)] = (c);

ive tried to search google, but ive been unsusscesful since # does not register on the search engine

i'll try to answer your question the best i can...
given the PUTPIXEL macro you gave, if you put that as a normal function that function would have to have direct access to the buffer.
so in actuality your function would like this:
void putPixel (int x, int y, int c, WORD* buffer) {   //code here....}

the function would need to be able to get to the buffer. in a macro, as the others have said it's cut and paste. plus SDL has already defined several things for you (such as the struct screen). so as opposed to looking up the function, it just plugs the macro in.

other reasons could be (but not necssarily true): speed and less memory used.

hopefully, i hit upon a few true points. but as usual if anyone sees anything wrong or incorrect, please correct (but read carefully first)

Beginner in Game Development?  Read here. And read here.

 

wow thanks guys, thorough and fast!
I think I got it down now, i was totally unaware that #<name> is actually not part of C++ but is another language.
gives me some new insight thanks! I have never met such a newbie friendly board for anything ever :P

I have real difficulty understanding the MSDN help page, as it defines terms using definitons im unfamiliar with. If I look up those unfamiliar terms, they're composed of definitions of other unfamiliar terms, recursivly repeat and then I get lost :)


This topic is closed to new replies.

Advertisement