Can someone explain this to me. I see it being used all the time, I am brand new to the language. Is it used to declare a certain type of a variable or constants or?
Can you use examples if possible, it's greatly appreciated.
Can someone explain this to me. I see it being used all the time, I am brand new to the language. Is it used to declare a certain type of a variable or constants or?
Can you use examples if possible, it's greatly appreciated.
jotun
n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]
think of #define as using constants, except they are can hold any type of data and can even hold operations.
For example:
Code:#define MY_NAME "Jeff" // Is basically the same as: const char name[]="Jeff";those are the most basic examples of #define, but you can also use #define for things called "macros" which are basically shortened ways of calling complex operations of code.Code:#define MY_AGE 16 // Is the same as const int age=16;
For example:
If you care to know what goes on in the background, basically during preprocessor-phase of the compiling, the compiler reads in all of these #define statements and remembers what they are, then, when you use them in your code, it in a sense "copies" whatever you had to the right of the definition in to your code.Code:#define CLEAN(data) { if(data) { delete [] data; data=NULL; } } int main() { int* dynamicMem=new int[34]; // We create the dynamic memory // Then, instead of having to call of the stuff to delete the memory, we can just use CLEAN CLEAN(dynamicMem); return 0; }
So in our example above, it would replace that CLEAN(dynamicMem) macro with the code like so:
Code:CLEAN(dynamicMem) // vvvvvvv // becomes // vvvvvvv { if(dynamicMem) { delete [] dynamicMem; dynamicMem=NULL; } }
#define will define something before compilation takes place, so constants are one use.Originally Posted by Jotun
ex)
#define PI (3.14)
....
float thing = 3 * PI;
// at compile time the "defined" value will be substituted in, ie) the compiler will see
float thing = 3 * (3.14);
#define can also be used for flags when including files. at the top of a header one might have something like
#ifndef MYFILE_H
#define MYFILE_H
// header
#endif
This is to ensure the file is only included once. The first time the file is encountered, MYFILE_H is not defined, so the next line is processed which will define MYFILE_H. If the file is included again, MYFILE_H will already be defined so the file is not processed twice.
These are just two examples of uses of #define. There are many others as well
Thank you, I appreciate the help guys.
jotun
n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]
Just because you can do something doesn't mean you should, though. It is bad practice to define constants using #define. Use the keyword const instead. Macros had greater usefullness in C, but since C++ has function templates, those are generally better to use than #defines. Remember that the preprocessor knows nothing about C++ or type checking. It will hurt you ruthlessly, and not raise the warnings it should.
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.