I have a struct (could be a class) which defined in ‘h’ file:
struct my_struct {
char * a;
char * b;
char * other_char;
char * hello;
// other 100 chars
// new chars can be added in future
};
I use this struct in my project. So I’m getting every attribute and value of this struct and call function:
void foo(char* attribute_name, char* attribute_value) {...}
Is there any way to dynamically get attributes names and values of the struct?
I need it because struct constantly raising, and I need to add code and recompile the project.
I need something like this:
void foo(my_struct s) {
int attributes = s.getAttrSize();
for (int i=0; i<attributes; ++i){
char* attribute_name = s.getAttrName[i];
char* attribute_value = s.getAttriValue[i];
}
}
thanks!
No. C++ does not have reflection, and this requirement indicates a possible poor design.
Variable names are given for convenience at the programming stage, and should not be taken as identifiers for data that exists at run-time.
However, you can create a real string->object mapping with
std::map.