Iterate through struct variables

后端 未结 4 575
孤独总比滥情好
孤独总比滥情好 2020-12-11 08:00

I want to get an iterator to struct variable to set a particular one on runtime according to enum ID. for example -

struct {
char _char;
int _int;
char* pcha         


        
4条回答
  •  星月不相逢
    2020-12-11 08:34

    No, C++ doesn't support this directly.

    You can however do something very similar using boost::tuple:

    enum {
    CHAR, //0
    INT,  //1
    DBL   //2
    };
    
    tuple t('b', 1, 3.14);
    
    int i = get(t);  // or t.get()
    

    You might also want to take a look at boost::variant.

提交回复
热议问题