Iterate through struct variables

后端 未结 4 578
孤独总比滥情好
孤独总比滥情好 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:45

    C++ doesn't support this. To iterate over struct members, you'll need some way to know what the struct members are. Compiled C++ programs don't posess this information. To them, a struct is just a collection of bytes.

    Languages like C# (anything .NET actually) and Java can do this because they store information about the structs (reflection information) along with the program.

    If you're really desperate for this feature, you could try to implement this by parsing the symbols file created by the compiler. This, however, is extremely advanced and it is unlikely that it is worth the effort.

提交回复
热议问题