自动创建已定义对象的向量?

时间:2017-12-21 14:40:47

标签: c++ c-preprocessor

假设我有一个在c ++中创建类成员的宏。有没有办法修改以下示例以自动创建NumbersILike::nums?假设创建的对象不是sizeof

#define MAKE_NUM(num) int num
class NumbersILike
{
    MAKE_NUM(three);
    MAKE_NUM(four);
    MAKE_NUM(five);
    std::vector<int*> nums = { &three,&four,&five };
};

1 个答案:

答案 0 :(得分:0)

这是可能的。我抬起头来看看X Macro&#39;。

class NumbersILike
{
public:
#define LIST_OF_ITEMS \
X(three) \
X(four) \
X(five)

#define X(nm) int nm;
    LIST_OF_ITEMS
#undef X

    std::vector<int*> derp = {
    #define X(nm) &nm,
            LIST_OF_ITEMS
    #undef X
    };
};