#define is preprocessor directive - it literally tells the preprocessor to replace n with 10 . const is actual C++ - it says that n is a constant of type int that is assigned the value 10. They'll do about the same thing, but are used for different reasons. In this case, n is a horrible choice for the variable name if #define is used because it could allow the preprocessor to insert 10's where you didn't mean for them to be. Another strike against the preprocessor is that it will define the constant globally - where as const follows scope restrictions native to C++.
Last edited by BlackJavaBean; Apr 6th, 2008 at 5:21 am. Reason: Added tags and update explanation
1. Type Checking:- macros are not type safe where as const are type safe. since the macros are replaced by definition at the time of preprocessing (which is done before compilation). the storage area of macros is not defined where as const has static storage area.
check the above code. the proper solution to above is to use templates rather than macros for specifying generic operatoins.
[see. Effective C++ for more details.].
so the conclusion is that use consts & templates where necessary don't use macros. .
Last edited by Laiq Ahmed; Apr 6th, 2008 at 8:25 am. Reason: forget to close code tag.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.