C#基于另一个#define错误的定义

Bus*_*ser 2 c c-preprocessor

因此,我的Visual Studio声明tag1和tag2均为未定义,但它们已明确定义,我不能基于另一个定义一个吗?

#define push                99
#define last_instruction    push

#ifdef DEBUG
    #define new_instr   (1+last_instruction) //should be 100
    #undef  last_instruction
    #define last_instruction   new_instr    //redifine to 100 if debug
#endif
Run Code Online (Sandbox Code Playgroud)

我在tag2上有一些案例,它说定义必须是const,但它是常量,它是1 + 99,将不胜感激。

谢谢!BA

Lun*_*din 5

首先,您不能两次定义相同的宏。如果需要替换宏,则首先必须执行以下操作#undef

#define tag1    99
#ifdef DEBUG
    #define tag2   (1+tag1)
    #undef tag1
    #define tag1   tag2
#endif
Run Code Online (Sandbox Code Playgroud)

但这并不能解决问题。宏不是变量,您不能使用它们存储值以供日后重用。它们是文本替换,因此它们并行存在。

因此,新定义#define tag1 tag2扩展为1+tag1。但是在这一点上,没有什么叫tag1,因为我们只是未定义它,并且还没有完成重新定义它。

对此进行过多思考,您会发疯:)因此,只需忘记整个事情,您真正想要做的就是:

#define tag1_val  99
#define tag1      tag1_val

#ifdef DEBUG
    #undef tag1
    #define tag1  (tag1_val+1)
#endif
Run Code Online (Sandbox Code Playgroud)