-
#define
What is it called when you do this:
#define whatever 1
And what is it called when you do this:
#define whatever(a,b) (a*b)
Both are macros, right? Is there something I can call the first example so that it doesn't apply for the second one? Maybe a simple macro? I think I remember hearing that someplace. Also, what would the second example be called?
I hope my question makes sense.
-
You can call them macros. It is just simple substitution done by the compiler with no error checking until it tries to compile the object later on.
YOu can change definitions like this:
Code:
#ifndef whatever
#define whatever 1
#endif
to change it later on in the code:
#ifdef whatever
#undef whatever
#endif
#define whatever((a,b) (a*b)
-
I guess a better way of asking my question is: Is the first example one kind of macro (one that doesn't take arguments) and the second example another kind of macro (one that does take arguments), or are they both just macros?
-
They are both substitutions or definitions. The name macro came later when you could pass arguments.
eg #define zout(x) memset(&x,0x0,sizeof(x) )
Retypes zout as the defined value and substitutes the x with whatever you supply as an argument.
The standard C name for this is a definition, hence #define
-
in C, the first thing is usually called a "symbolic constant" because you are giving a constant (in this case 1) a symbolic name (whatever).
The second thing is called a macro because it's a small piece of code that will be inserted instead of it's name.
But both are simply text substitutions. In C++, you should replace all symbolic constants with real typed constants:
const int whatever = 1;
And macros with inline templates:
template <class T>
inline T whatever(T a, T b)
{
return a*b;
}
In most cases it will work exactly the same.