Ack! Don't get caught up in this!
Macros are evil. Refrain from using them. Especially for making large blocks of code.
Functions and const variables are always better to use when possible for many reasons.
per your original problem:
1 2 3 4 5 6 7
|
// bad:
#define myAlias "Hello World"
cout << myAlias;
// good:
static const char* myAlias = "Hello World";
cout << myAlias;
|
You can use macros to expand blocks of code as well.. but again you should refrain from doing this:
1 2 3 4 5 6
|
// bad:
#define SORT_VEC(v) sort( v.begin(), v.end() )
// good:
template<typename T>
inline void SORT_VEC(T& v) { sort( v.begin(), v.end() ); }
|
Macros are bad because:
1) They ignore scope rules (pollute namespace)
2) They expand code that might be better left unexpanded (inlining stuff that shouldn't be inlined)
3) They can lead to unexpected errors (both compiletime and runtime) if you're not very very careful
4) They ignore C++ rules because they're processed before the C++ compiler starts
5) They are not recognizable symbols in the debugger
6) You can't get a pointer to a constant defined by #define if you need it
7) other reasons
Sometimes they are the best option -- but not in any of the instances mentioned in this thread. Refrain from using them whenever possible.