C / C ++可能获得]清单"实例成员通过查询类? [英] C/C++ Possible to get a "list" of instance members by querying a class?

查看:76
本文介绍了C / C ++可能获得]清单"实例成员通过查询类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有C ++中的结构:

Suppose we have a struct in C++:

struct foobar
{
      int age; 
      bool hot;
      String name
};

有没有一种方法,编程,查询上述结构来提取其实例成员?例如:

Is there a way, programatically, to query the above struct to extract its instance members? For example:

String[] members = magicClass.getInstanceMembers(foobar);

成员将有 [年龄,热,名] ,因为它的值。

可能吗?为什么我想问的原因是因为我有随时间变化的结构(变量添加/删除)。我希望能够建立自动生成Lua的文件,这个保存的数据。

Possible? The reason why I ask is because I have structs that change over time (variables added/removed). I want to be able to create auto-generating Lua files with this saved data.

感谢

推荐答案

没有,标准C ++不支持这种类型的反思。有使用宏来创建将使用 SFINAE 静态确定是否类型特征式的模板一些哈克的方式还是不特定类有一定的数据成员或成员函数,但没有将实际枚举类的每一个成员。

No, standard C++ doesn't support that type of reflection. There are some "hacky" ways using macros to create a type-traits-esque template that will use SFINAE to statically determine whether or not a particular class has a certain data member or member function, but nothing that will actually enumerate every member of a class.

事实上,C ++在设计时考虑到一定的经营理念,将使它很难,如果不是适得其反,支持运行时反射我们在高级语言如C#/ Java的见的类型。请参见为什么C ++没有反映?获取深入讨论在这个问题上。

In fact, C++ was designed with a certain philosophy in mind that would make it difficult, if not counter-productive, to support the type of runtime reflection we see in higher-level languages like C#/Java. See Why does C++ not have reflection? for a thorough discussion on this issue.

这篇关于C / C ++可能获得]清单"实例成员通过查询类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆