#define命名空间中的语句 [英] #define statements within a namespace
问题描述
如果我在这样的名称空间中有一个#define语句:
If I have a #define statement within a namespace as such:
namespace MyNamespace
{
#define SOME_VALUE 0xDEADBABE
}
我是否正确地说#define语句不限于名称空间?
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;
}
推荐答案
正确,#define
不受名称空间的约束. #define
是预处理器指令-它导致在通过编译之前对源文件进行操作编译器.在编译步骤中使用了命名空间,并且编译器无法深入了解#define
.
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.
您应尽量避免使用预处理器.对于像这样的常量值,最好使用const而不是#define
.
You should try to avoid the preprocessor as much as possible. For constant values like this, prefer const over #define
's.
这篇关于#define命名空间中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!