2012-04-02 99 views
2

随着使用普通Ç预处理宏,是有可能创造这样的:自动增加宏扩展

INIT_BASE(0x100)      // init starting number 

#define BASE_A GET_NEXT_BASE   // equivalent to #define BASE_A 0x101 
#define BASE_B GET_NEXT_BASE   // 0x102 
#define BASE_C GET_NEXT_BASE   // 0x103 
+6

为什么它需要一个预处理命令#define?为什么不只是一个枚举? – 2012-04-02 15:44:39

回答

3

宏不能自动执行该类型的计数,但enum s可以。

#define INIT_BASE 0x100 
enum foo 
{ 
    BASE_A = INIT_BASE + 1, 
    BASE_B, 
    BASE_C, 
    ... 
}; 

除非你真的使用宏,你将不得不做手工计数:

#define INIT_BASE 0x100 
#define BASE_A (INIT_BASE + 1) // equivalent to #define BASE_A 0x101 
#define BASE_B (INIT_BASE + 2) // 0x102 
#define BASE_C (INIT_BASE + 3) // 0x103 
+0

...并且你想在'INIT_BASE + N'表达式周围放置圆括号。 – 2012-04-02 15:55:37

+0

美丽!我错误地认为枚举在运行时占用内存。 – Saideira 2012-04-02 17:00:06

3

你试过:

#define BASE_A (INIT_BASE+1) // equivalent to #define BASE_A 0x101 
#define BASE_B (BASE_A+1)   // 0x102 
#define BASE_C (BASE_B+1)   // 0x103