我可以这样使用#undef吗?
我想获取存储在注册表中的一些设置,如果它们与 #define
不同,我想重新定义它,我可以这样做吗?:
#define DEFINED_X "testSetting"
void LoadConfig()
{
regConfigX = some value previusly stored in the registry;
if(regConfigX!=DEFINED_X)
{
#undef DEFINED_X
#define DEFINED_X regConfigX
}
}
我认为 #define 仅在编译时使用,该代码在运行编译后的 exe 时是否有效?
I want to get some settings I store in the registry, and if they differ from a #define
I want to redefine it, could I do it this way?:
#define DEFINED_X "testSetting"
void LoadConfig()
{
regConfigX = some value previusly stored in the registry;
if(regConfigX!=DEFINED_X)
{
#undef DEFINED_X
#define DEFINED_X regConfigX
}
}
I tought #define
was used only when compiling, would this code work when running the compiled exe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。
#define
和#undef
是预处理指令;它们在编译源代码之前进行评估。为此,您需要使用变量,而不是宏。
No.
#define
and#undef
are preprocessing directives; they are evaluated before the source code is compiled.You need to use a variable for this, not a macro.
#define
和#undef
发生在源代码到达编译器之前。与#define 相关的任何事情都不会在运行时发生。您还应该查看 Boost 预处理器库。
#define
and#undef
occur before your source code even hits the compiler. Anything to do with#define
s can't happen at runtime.You should check out the Boost preprocessor library, too.
不,使用静态变量来存储 DEFINED_X 的值。
No, use a static variable to store the value of DEFINED_X.