#define statements within a namespace

前端 未结 5 1558
旧巷少年郎
旧巷少年郎 2021-02-01 12:34

If I have a #define statement within a namespace as such:

namespace MyNamespace
{
  #define SOME_VALUE 0xDEADBABE
}

Am I correct in saying that

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 13:14

    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.

提交回复
热议问题