|
-
Feb 18th, 2002, 10:18 AM
#1
#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.
-
Feb 18th, 2002, 10:41 AM
#2
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)
-
Feb 18th, 2002, 04:07 PM
#3
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?
-
Feb 18th, 2002, 05:19 PM
#4
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
-
Feb 18th, 2002, 05:30 PM
#5
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|