Advertisement

#define vs const

Started by March 22, 2001 07:02 PM
23 comments, last by thuned 23 years, 10 months ago
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
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

#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



"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
Advertisement
- 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.
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
so when should i use define?

thuned

life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
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):
  #ifndef MYFUNCTION_FILE_IS_INCLUDED#define MYFUNCTION_FILE_IS_INCLUDED#define MyMacro(a) DoSomething(a)void MyFunction(int abc) {  #ifndef MY_DEBUG_OPT // If it isn''t defined do the following    SomeOutputToFile("MyFunction called with x=%d",abc);  #endif // Close the preprocessor if statement  MyMacro(abc);}#endif  

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.gdarchive.net/druidgames/
Advertisement
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.


Windows SUCKS! Deal with it!
if(windows crashes)
{
run Linux
}
else
{
yea right!!
}

Resist Windows XP''s Invasive Product Activation Technology!
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 :
#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


"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.gdarchive.net/druidgames/

This topic is closed to new replies.

Advertisement