重新定义或更改宏值 [英] Redefining or changing macro value

查看:1133
本文介绍了重新定义或更改宏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个已经开发的项目,用MFC C ++编写,并且遇到一个已经存在的宏定义的问题:

  #define HEIGHT_TESTS 13 

我试图从代码中更改值,因为它是一个预处理的定义,我不能这样做。有一种方法,我可以解决这个问题,而不必改变原始的宏整体(因为它可能会影响程序的原始功能)。我只是打算改变它在一个特定的条件,休息在其他地方它保持不变。



只是为了让大家知道,我已经试过使用一个不同的宏定义与值(17)我打算使用,但没有运气这样。



任何帮助将非常感激。

解决方案

您可以 undef ine it和 define

  #include< iostream& 

#define AAA 13

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

输出: 7



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



...这是为什么你应该避免使用宏和 #define s,它们可以由静态函数和变量替换:)








查看此代码:

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

预处理器逐行从顶部到底部, / p>


  • ah, #define AAA 13 下次我打 AAA 我会把 13

  • em> look,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 将不会使用。


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.

解决方案

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.

这篇关于重新定义或更改宏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆