重新定义或改变宏观价值
我目前正在开发一个用 MFC C++ 编写的已开发项目,并且面临一个已存在的具有定义的宏的问题:
#define HEIGHT_TESTS 13
我试图从代码中更改值,但我认为由于它是一个预处理的定义,我无法这样做。有没有一种方法可以解决这个问题,而不必整体更改原始宏(因为它可能会影响程序的原始功能)。我只是打算在一种特定条件下改变它,其他地方保持不变。
只是为了让大家知道,我显然已经尝试使用不同的宏定义以及我打算使用的值 (17),但没有运气。
任何帮助将不胜感激。
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:
#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.
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以
undef
ine它并再次define
:输出:
7
请注意,以
#
开头的语句是<在编译代码之前就处理的strong>预处理器指令。在这种情况下,这个常量AAA
将被简单地替换为7
,即它的工作原理就像文本替换一样,不需要额外的语法检查,没有类型安全等......这就是为什么您应该避免使用宏和#define 的主要原因,因为它们可以被静态函数和变量替换:)
为什么要“文本替换”?
看看在这段代码中:
预处理器从上到下逐行进行,这样做:
#define AAA 13
,所以当我下次点击AAA
时,我会在那里放置13
AAA
,我将其替换为13
7
,所以我将停止使用13
redefAndPrint()
中,我将把7
放在那里,将给定的代码转换为这个:
它将输出
13713
并且最新的#define AAA 4
根本不会被使用。You can
undef
ine it anddefine
again:outputs:
7
Please note that statements that start with
#
are preprocessor directives that are taken care of before the code is even compiled. In this case, this constantAAA
will be simply replaced by7
, i.e. it works just like a textual replacement with no additional checks of syntax, no type safety etc......which is main reason why you should avoid using macros and
#define
s where they can be replaced by static functions and variables :)Why "textual replacement" ?
Look at this code:
preprocessor goes line by line from the top to the bottom, doing this:
#define AAA 13
, so when I hitAAA
next time, I'll put there13
AAA
, I'm replacing it with13
7
, so I'll stop using13
redefAndPrint()
I'll put there7
transforming the given code into this one:
which will output
13713
and the latest#define AAA 4
won't be used at all.像下面这样:
Something like the following: