我有一个在'h'文件中定义的结构(可能是一个类):
struct my_struct {
char * a;
char * b;
char * other_char;
char * hello;
// other 100 chars
// new chars can be added in future
};
Run Code Online (Sandbox Code Playgroud)
我在我的项目中使用这个结构.所以我得到了这个结构和调用函数的每个属性和值:
void foo(char* attribute_name, char* attribute_value) {...}
Run Code Online (Sandbox Code Playgroud)
有没有办法动态获取结构的属性名称和值?
我需要它,因为struct不断提高,我需要添加代码并重新编译项目.
我需要这样的东西:
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];
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
没有.C++没有反射,这个要求表明可能设计不佳.
在编程阶段为方便起见,给出了变量名,不应将其作为运行时存在的数据的标识符.
但是,您可以创建一个真正的字符串 - >对象映射std::map.
| 归档时间: |
|
| 查看次数: |
222 次 |
| 最近记录: |