宏观技巧...
如果要在 C 宏中展开循环,则需要具有与迭代一样多的宏(使用预处理器,您还可以通过递归包含文件来玩其他技巧,但是您没有类似接口的宏)。
我已经添加了结束标记,我不知道它是否真的需要。
我确实通过构建宏名称来分派到一个空的结束宏或一个连续的宏。
所以这里有一些你想做的事情——使用修改后的界面和最多 4 个对象。只需为更多对象添加类似的宏。
#define TYPE_DEF_OBJECT(name, class, constructor_params) \
typedef class name ## type; \
name ## type* name;
#define TYPE_DEF_END(name, class, constructor_params)
#define TYPE4_OBJECT(name, class, constructor_params) \
TYPE_DEF_OBJECT(name, class, constructor_params) TYPE5
#define TYPE4_END(name, class, constructor_params) \
TYPE_DEF_END(name, class, constructor_params)
#define TYPE4(kind, name, class, constr) \
TYPE4_##kind(name, class, constr)
#define TYPE3_OBJECT(name, class, constructor_params) \
TYPE_DEF_OBJECT(name, class, constructor_params) TYPE4
#define TYPE3_END(name, class, constructor_params) \
TYPE_DEF_END(name, class, constructor_params)
#define TYPE3(kind, name, class, constr) \
TYPE3_##kind(name, class, constr)
#define TYPE2_OBJECT(name, class, constructor_params) \
TYPE_DEF_OBJECT(name, class, constructor_params) TYPE3
#define TYPE2_END(name, class, constructor_params) \
TYPE_DEF_END(name, class, constructor_params)
#define TYPE2(kind, name, class, constr) \
TYPE2_##kind(name, class, constr)
#define TYPE1_OBJECT(name, class, constructor_params) \
TYPE_DEF_OBJECT(name, class, constructor_params) TYPE2
#define TYPE1_END(name, class, constructor_params) \
TYPE_DEF_END(name, class, constructor_params)
#define TYPE1(kind, name, class, constr) \
TYPE1_##kind(name, class, constr)
#define TYPE0_OBJECT(name, class, constructor_params) \
TYPE_DEF_OBJECT(name, class, constructor_params) TYPE1
#define TYPE0_END(name, class, constructor_params) \
TYPE_DEF_END(name, class, constructor_params)
#define TYPE(kind, name, class, constr) \
TYPE0_##kind(name, class, constr)
#define RUN_DEF_OBJECT(name, class, constructor_params) \
name ## type* name = new name##type constructor_params;
#define RUN_DEF_END(name, class, constructor_params)
#define RUN4_OBJECT(name, class, constructor_params) \
RUN_DEF_OBJECT(name, class, constructor_params) RUN5
#define RUN4_END(name, class, constructor_params) \
RUN_DEF_END(name, class, constructor_params)
#define RUN4(kind, name, class, constr) \
RUN4_##kind(name, class, constr)
#define RUN3_OBJECT(name, class, constructor_params) \
RUN_DEF_OBJECT(name, class, constructor_params) RUN4
#define RUN3_END(name, class, constructor_params) \
RUN_DEF_END(name, class, constructor_params)
#define RUN3(kind, name, class, constr) \
RUN3_##kind(name, class, constr)
#define RUN2_OBJECT(name, class, constructor_params) \
RUN_DEF_OBJECT(name, class, constructor_params) RUN3
#define RUN2_END(name, class, constructor_params) \
RUN_DEF_END(name, class, constructor_params)
#define RUN2(kind, name, class, constr) \
RUN2_##kind(name, class, constr)
#define RUN1_OBJECT(name, class, constructor_params) \
RUN_DEF_OBJECT(name, class, constructor_params) RUN2
#define RUN1_END(name, class, constructor_params) \
RUN_DEF_END(name, class, constructor_params)
#define RUN1(kind, name, class, constr) \
RUN1_##kind(name, class, constr)
#define RUN0_OBJECT(name, class, constructor_params) \
RUN_DEF_OBJECT(name, class, constructor_params) RUN1
#define RUN0_END(name, class, constructor_params) \
RUN_DEF_END(name, class, constructor_params)
#define RUN(kind, name, class, constr) \
RUN0_##kind(name, class, constr)
#define DEF_OBJECTS(definitions) \
TYPE definitions \
RUN definitions \
DEF_OBJECTS(
(OBJECT, Object1, CClass1, ("par1"))
(END, , , )
)