如果我在这样的命名空间中有 #define 语句:
namespace MyNamespace
{
#define SOME_VALUE 0xDEADBABE
}
我说#define 语句不限于命名空间是否正确?
以下是“正确”的做法吗?
namespace MyNamespace
{
const unsigned int SOME_VALUE = 0xDEADBABE;
}
最佳答案
正确,#define
不受命名空间的约束。 #define
是 preprocessor指令 - 它导致在通过编译器编译之前对源文件进行操作。命名空间在编译步骤中使用,编译器无法深入了解 #define
的内容。
您应该尽量避免使用预处理器。对于像这样的常量值,最好使用 const 而不是 #define
的。
关于c++ - #define 命名空间中的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1088290/