define Vs. const
-
Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!
-
Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!
You should not consider to use #define to create a constant.
#define
define a macro. That mean, it will be used by preprocessor. There is not type checking here. The identifier, that appear after define keyword, exists only during preprocessing.const
define a constant. That mean, it will be used by compiler. Therefore, compiler do alot base on that define. -
Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!
- Regarding to the very purpose in your question, the main difference is ~ scope. A define lasts its definition till the end of the compilation module; but a const literal only lives within its scope. For example, try to compile the code below:
void Foo()
{
#define MAX_SIZE 192
// ...
}// ...
bool Bar()
{
#define MAX_SIZE 512 // Oops!
int iSize = MAX_SIZE;
}When your program is very large with a lot of source files, and the define(s) hide somewhere very deep in some header file(s) included by the other header files, you would find it annoying, because it needs to undef and ...... 2) There are some other reasons about avoiding macros (not related to your question). Please read Section 7.8 - Macro in the book The C++ Programming Language, 3rd by Stroustrup. "Macros are very important in C but have far fewer uses in C++. The first rule about macros is: Don't use them unless you have to. Almost every macro demonstrates a flaw in the programming language, in the program, or in the programmer. Because they rearrange the program text before the compiler proper sees it, macros are also a major problem for many programming tools. ...... " ~ Stroustrup. The section demonstrates many flaws! Maxwell Chen
-
Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!
-
Hi all, ibe been coding for over a year now, and ibe never actually stopped to think about this (duh). if i need to define a constant value like a string, a number, etc. for my app should i: #define MY_STRING _T("this is a string") #define MY_NUMBER 100 or: const TCHAR* MY_STRING = _T("this is a string"); const int MY_NUMBER = 100; and why? needless to say, both approaches work fine, and most code ibe seen uses the #define approach, however, a book i have (Beginning Visual C++ by Ivor Horton) sais that i should use the "const" approach, and that the #define one is obsolete and should be avoided (doesnt mention why tho) Thanks!
Be aware that const may allocate memory. To say #define is obsolete is idiotic. It's a tool that is extremely useful. I prefer it not be used for strings, but on occasion it's the best solution for a problem. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke