C++ 重新定义或更改宏值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9274500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redefining or changing macro value
提问by Neophile
I am currently working on an already developed project written in MFC C++ and am facing a problem with an already present macro having the definition:
我目前正在开发一个用 MFC C++ 编写的已经开发的项目,并且面临一个已经存在的具有定义的宏的问题:
#define HEIGHT_TESTS 13
I am trying to change the value from within the code but I think since its a preprocessed definition, I am unable to do that. Is there a way I could get around this problem without having to change the original macro overall (as it might affect the original functionality of the program). I am just intending to change it in one particular condition, rest everywhere else it remains the same.
我试图从代码中更改值,但我认为由于它是一个预处理定义,我无法做到这一点。有没有一种方法可以解决这个问题,而不必整体更改原始宏(因为它可能会影响程序的原始功能)。我只是想在一个特定的条件下改变它,在其他任何地方都保持不变。
Just to let everyone know, I have obviously tried out using a different macro definition with the value (17) I am intending to use, but no luck as such.
只是为了让大家知道,我显然已经尝试使用不同的宏定义和我打算使用的值 (17),但没有运气。
Any help would be much appreciated.
任何帮助将非常感激。
回答by LihO
You can undef
ine it and define
again:
您可以undef
INE并define
再次:
#include <iostream>
#define AAA 13
int main()
{
#undef AAA
#define AAA 7
std::cout << AAA;
}
outputs: 7
输出: 7
Please note that statements that start with #
are preprocessor directivesthat are taken care of before the code is even compiled. In this case, this constant AAA
will be simply replaced by 7
, i.e. it works just like a textual replacement with no additional checks of syntax, no type safety etc...
请注意,开始与该语句#
是预处理器指令所采取的代码之前甚至编译护理。在这种情况下,这个常量AAA
将被简单地替换为7
,即它就像文本替换一样工作,没有额外的语法检查,没有类型安全等......
...which is main reason why you should avoid using macros and #define
s where they can be replaced by static functions and variables :)
...这是您应该避免使用宏和#define
s 的主要原因,因为它们可以被静态函数和变量替换:)
Why "textual replacement" ?
为什么是“文本替换”?
Look at this code:
看看这段代码:
#include <iostream>
#define AAA 13
void purePrint() {
std::cout << AAA;
}
void redefAndPrint() {
#undef AAA
#define AAA 7
std::cout << AAA;
}
int main()
{
#undef AAA
#define AAA 4
purePrint();
redefAndPrint();
purePrint();
}
preprocessor goes line by line from the top to the bottom, doing this:
预处理器从上到下一行一行,这样做:
- ah,
#define AAA 13
, so when I hitAAA
next time, I'll put there13
- look, purePrint uses
AAA
, I'm replacing it with13
- wait, now they tell me to use
7
, so I'll stop using13
- so here in
redefAndPrint()
I'll put there7
- 啊,
#define AAA 13
所以AAA
下次我打的时候,我会放在那里13
- 看, purePrint 使用
AAA
,我将其替换为13
- 等等,现在他们告诉我要使用
7
,所以我将停止使用13
- 所以在这里
redefAndPrint()
我会放那里7
transforming the given code into this one:
将给定的代码转换为以下代码:
#include <iostream>
void purePrint() {
std::cout << 13;
}
void redefAndPrint() {
std::cout << 7;
}
int main()
{
purePrint();
redefAndPrint();
purePrint();
}
which will output 13713
and the latest #define AAA 4
won't be used at all.
这将输出13713
,而最新的#define AAA 4
根本不会被使用。
回答by hmjd
Something like the following:
类似于以下内容:
#undef HEIGHT_TESTS
#define HEIGHT_TESTS 17
// Use redefined macro
// Restore
#undef HEIGHT_TESTS
#define HEIGHT_TESTS 13