#define statements within a namespace

c++, namespaces

Solution

Correct,`#define`'s aren't bound by namespaces. `#define` is a preprocessor directive - it results in manipulation of the source file prior to being compiled via the compiler. Namespaces are used during the compilation step and the compiler has no insight into the `#define`'s.

You should try to avoid the preprocessor as much as possible. For constant values like this, prefer const over `#define`'s.

Problem

If I have a #define statement within a namespace as such: ``` namespace MyNamespace { #define SOME_VALUE 0xDEADBABE } ``` Am I correct in saying that the #define statement is not restricted to the namespace? Is the following the "correct" thing to do? ``` namespace MyNamespace { const unsigned int SOME_VALUE = 0xDEADBABE; } ```

Original source