重新定义或改变宏观价值

发布于 2025-01-04 22:59:59 字数 293 浏览 4 评论 0原文

我目前正在开发一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一刻暧昧 2025-01-11 22:59:59

您可以undefine它并再次define

#include <iostream>

#define AAA 13

int main()
{
    #undef AAA
    #define AAA 7
    std::cout << AAA;
}

输出:7

请注意,以#开头的语句是<在编译代码之前就处理的strong>预处理器指令。在这种情况下,这个常量 AAA 将被简单地替换为 7,即它的工作原理就像文本替换一样,不需要额外的语法检查,没有类型安全等...

...这就是为什么您应该避免使用宏和#define 的主要原因,因为它们可以被静态函数和变量替换:)


为什么要“文本替换”?

看看在这段代码中:

#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();
}

预处理器从上到下逐行进行,这样做:

  • 啊,#define AAA 13,所以当我下次点击AAA时,我会在那里放置13
  • 看,purePrint 使用 AAA,我将其替换为 13
  • 等等,现在他们告诉我使用 7,所以我将停止使用13
  • 所以在 redefAndPrint() 中,我将把 7 放在那里,

将给定的代码转换为这个:

#include <iostream>

void purePrint() {
    std::cout << 13;
}

void redefAndPrint() {
    std::cout << 7;
}

int main()
{
    purePrint();
    redefAndPrint();
    purePrint();
}

它将输出 13713 并且最新的 #define AAA 4 根本不会被使用。

You can undefine it and define again:

#include <iostream>

#define AAA 13

int main()
{
    #undef AAA
    #define AAA 7
    std::cout << AAA;
}

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 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...

...which is main reason why you should avoid using macros and #defines where they can be replaced by static functions and variables :)


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 hit AAA next time, I'll put there 13
  • look, purePrint uses AAA, I'm replacing it with 13
  • wait, now they tell me to use 7, so I'll stop using 13
  • so here in redefAndPrint() I'll put there 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.

我不吻晚风 2025-01-11 22:59:59

像下面这样:

#undef HEIGHT_TESTS
#define HEIGHT_TESTS 17

// Use redefined macro

// Restore
#undef HEIGHT_TESTS
#define HEIGHT_TESTS 13

Something like the following:

#undef HEIGHT_TESTS
#define HEIGHT_TESTS 17

// Use redefined macro

// Restore
#undef HEIGHT_TESTS
#define HEIGHT_TESTS 13
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文