![](smile.gif)
#define vs const
what''s the difference? in which cases would i use something like
const int x = 4;
and #define x 4
?
thuned
life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
![](smile.gif)
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
Someone correct me if I am wrong, but won''t the code
change all x''s to 4''s in the preprocessing? While :
x is stored in memory and it is accessed like any other variable.
I am not 100% sure on this, and I am interested in an experienced persons reply![](smile.gif)
"You don''''t get wise with the sleep still in your eyes." - Neil Peart
#define x 4
change all x''s to 4''s in the preprocessing? While :
const int x = 4;
x is stored in memory and it is accessed like any other variable.
I am not 100% sure on this, and I am interested in an experienced persons reply
![](smile.gif)
"You don''''t get wise with the sleep still in your eyes." - Neil Peart
"You won't get wise with the sleep still in your eyes." - Neil Peart
- They compile to the same code in MSVC++ 6.0.
- The #define has global namespace and no type.
- The const can be put inside a namespace or class, and has type so it can be checked for correct type conversions.
For these reasons, you should avoid using #define for constants--favor const variables instead.
- The #define has global namespace and no type.
- The const can be put inside a namespace or class, and has type so it can be checked for correct type conversions.
For these reasons, you should avoid using #define for constants--favor const variables instead.
A global const is better than a define because there is no type checking on defines. I think your right Mucman, if i remember, whenever the compiler sees the x it will just replace it with 4.
ECKILLER
ECKILLER
ECKILLER
so when should i use define?
thuned
life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
thuned
life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
![](smile.gif)
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
You use define when you want the preprocessor to use it, or to make a macro.
For example you couldn''t do this with const variables (there are ways to do some of this without consts or defines though):
Those are the some of the possible uses of defines. I''m not saying they''re better or worse, but they still have their place.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
For example you couldn''t do this with const variables (there are ways to do some of this without consts or defines though):
|
Those are the some of the possible uses of defines. I''m not saying they''re better or worse, but they still have their place.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
quote:
Original post by Stoffel
- They compile to the same code in MSVC++ 6.0.
- The #define has global namespace and no type.
- The const can be put inside a namespace or class, and has type so it can be checked for correct type conversions.
For these reasons, you should avoid using #define for constants--favor const variables instead.
If I remember right #define is a C relic and C++ uses const instead of #define and for the reasons Stoffel posted is why you should use it instead.
![](smile.gif)
Windows SUCKS! Deal with it!
if(windows crashes)
{
run Linux
}
else
{
yea right!!
}
![Resist Windows XP''s Invasive Product Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
I have another question on the lines of this topic... why would you define macros when you could use inline functions? I had an assigment question where there was a define called :
Now try changing the macros to inline functions and it will work properly...
I guess that doesn''t answer the question about when to use define macros, but it does show you when not to use them![](wink.gif)
"You don''''t get wise with the sleep still in your eyes." - Neil Peart
#include #define max(a, b) (((a) > (b)) ? (a) : (b))#define min(a, b) (((a) < (b)) ? (a) : (b))int main(){ int x = 100; int y = 99; int z; z = max(min(--x, y--), min(++y, x++)); cout << "x=" << x << " y=" << y << " z=" << z << endl; return 0;}
Now try changing the macros to inline functions and it will work properly...
I guess that doesn''t answer the question about when to use define macros, but it does show you when not to use them
![](wink.gif)
"You don''''t get wise with the sleep still in your eyes." - Neil Peart
"You won't get wise with the sleep still in your eyes." - Neil Peart
quote:
I think your right Mucman, if i remember, whenever the compiler sees the x it will just replace it with 4.
Whenever it sees the x alone that is. If you had a variable called plax or some comment with the word plax in it, it wouldn''y replace it, otherwise i think so.
The compiler has an option of ignoring your inline functions, I use macros where the code absolutely must be inserted there (I''ll have to dig up an example, not sure where I put that project anymore). I did a test a while ago, and inline functions were slower that macros in MSVC until I changed the Function Inlining setting to All Applicable. So evidently MSVC was choosing to ignore my inlines.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement