Hi,
can any one give proper reason why should i use const as compaire to macro ?
Hi,
can any one give proper reason why should i use const as compaire to macro ?
An evil header file can redefine your macro without your knowledge. A header file trying to redefine your const will throw a compile error.
You can take the address of a const -- can't do that with a macro.
Why use a hack (macros) when there are finally better methods available?
Yes. Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
the only legitimate use of #macro preprocessor seems to be for skipping unneeded parts at compile time.
for example, use #ifdef etc, but, that's all
there are much better ways in C++ than to do this silly thing:
#define PI 3.14if0rg0t
No, macros have so much more use than that, but they are also evil at the same time, so avoiding them and using them wisely is a good idea.
(They can, for example, increase readability by picking away large chunks of code that can't be shortened by typedefs [for example, templates], among other things.)
Also, macros can't be separated by namespaces, so macro name collisions are much more common than variables separated by namespaces.
>> Tools are not innately evil, people can only use the in evil ways.
Calling a tool "evil" that makes it unecessarily easy for users to use it in evil ways is acceptable.
Pointers and references are evil, too, then I guess. C++ is an evil language. Look at the majority of questions on the forum. People doing careless things easily.
Blame the carpenter, not the tool.
Do pointers and references make it unnecessarily easy for a user to use them evilly? Are there alternatives that are harder to do evil with?
Blame the tool when the tool deserves blame. If you buy a nail gun that shoots the nails in both directions, then that nail gun is evil. Yes, you should be careful and read the directions and only use that nail gun when you have to drive a nail into two different directions, but the tool still certainly deserves blame for all the people with nails in their eye.
[strawman argument elided]
Sure, there are alternatives - use Java or C# instead. The overwhelming majority of people that use C++ aren't disciplined or responsible enough to use it and would be better served with a simpler language. I'm tired of seeing the language criticized because of the actions of lazy and/or bad programmers.
It doesn't sound like you understood what I was trying to get at. Then again, you accuse me of using a strawman argument, so perhaps I'm not understanding you. I think you implied that a tool cannot be considered evil. I am claiming that a tool can be considered evil.
References are not considered evil because they do not unnecessarily allow users to do bad things with them, and there are no alternatives that are less evil.
Macros are considered evil because they do allow users to do evil (by polluting namespaces, ignoring type rules, etc) and there are generally better alternatives (const, inline, etc).
So, for someone to call a tool (in this case a feature of C++) evil is completely acceptable because there is an inherent difference between the evil tool and the not evil alternative.
Last edited by Daved; 05-16-2008 at 11:57 AM.
Well I think Java is evil.
But then again, there are a few parts of C++ I would have liked to have seen done differently, but some of them would have broken compatability with C... Why that's such a bad thing, I'm not sure; just name your C files .c and C++ files .cpp and they should use the right rules for compiling each (but then you'd need to use .hpp files instead of .h for C++).
Another reason is that you can't change the value of a macro at run time but you can change the value of a const at run time (if you do it right and set things up properly)