可能重复:
Can I redefine a c++ macro for a few includes and then define it back?
我有以下内容:
#include <iostream>
#define A 1 // define a macro
#define B A // copy macro? (note this does not work...)
#undef A // undefine first macro
#define A 2 // redefine it as something else
int main (int argc, const char * argv[])
{
std::cout << A << " " << B;
return 0;
}
我的程序打印“2 2”。我想要它打印“2 1”。我该怎么做呢? (顺便说一下,请原谅我在 C 问题中混合使用C ++。我真的只关心问题的预处理器部分,我希望它符合C99标准。)
最终我想做点什么:
// In file A
#define A 1 // define a macro
// In file B
#include "fileA.h"
#define B A // copy macro?
#undef A // undefine first macro
#define A 2 // redefine it as something else
// #include other files using A
#define A B // set macro back to original value, whatever that was.
更新 - 已经在这里回答: