对象反射

发布于 2024-08-07 16:01:21 字数 102 浏览 3 评论 0原文

有人有关于用 C++ 构建完整的对象/类反射系统的参考吗?

我见过一些疯狂的宏/模板解决方案,但是我从未找到一个系统可以将所有问题解决到我满意的水平。

谢谢!

Does anyone have any references for building a full Object/Class reflection system in C++ ?

Ive seen some crazy macro / template solutions however ive never found a system which solves everything to a level im comfortable with.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2024-08-14 16:01:21

使用模板和宏自动或半自动定义所有内容几乎是 C++ 中的唯一选择。 C++ 的反射/内省能力非常弱。但是,如果您想做的主要是序列化和存储,那么这已经在 Boost 序列化库。您可以通过实现 序列化器方法来完成此操作 在课程上,或者有如果您不想修改该类,则可以使用外部函数

但这似乎不是您要问的。我猜您想要类似自动序列化的东西,这不需要类实现者付出额外的努力。他们在 PythonJava 和许多其他语言,但不包括 C++。为了得到你想要的,你需要实现你自己的对象系统,就像 IgKh 在 他的回答

如果你想这样做,我建议你看看 JavaScript 如何实现对象。 JavaScript 使用基于原型的对象系统,它相当简单,但相当强大。我推荐这样做是因为在我看来,如果您必须自己做的话,实施起来会更容易。如果您有心情阅读有关原型优点和优雅的冗长解释,您可以找到Steve Yegge 博客上有关该主题的文章。他是一位非常有经验的程序员,所以我相信他的意见,但我自己从来没有这样做过,所以我只能指出别人所说的。

如果您想保留更多 C++ 风格的类和实例,而不是不太熟悉的原型,请查看 Python 对象序列化工作。 Python也使用“属性”方法来实现其对象,但属性用于实现类和继承,而不是基于原型的系统,因此它可能更熟悉一些。

抱歉,我没有更简单的答案来回答您的问题!但希望这会有所帮助。

Using templates and macros to automatically, or semi-automatically, define everything is pretty much the only option in C++. C++ has very weak reflection/introspection abilities. However, if what you want to do is mainly serialization and storage, this has already been implemented in the Boost Serialization libraries. You can do this by either implementing a serializer method on the class, or have an external function if you don't want to modify the class.

This doesn't seem to be what you were asking though. I'm guessing you want something like automatic serialization which requires no extra effort on the part of the class implementer. They have this in Python, and Java, and many other languages, but not C++. In order to get what you want, you would need to implement your own object system like, perhaps, the meta-object system that IgKh mentioned in his answer.

If you want to do that, I'd suggest looking at how JavaScript implements objects. JavaScript uses a prototype based object system, which is reasonably simple, yet fairly powerful. I recommend this because it seems to me like it would be easier to implement if you had to do it yourself. If you are in the mood for reading a VERY long-winded explanation on the benefits and elegance of prototypes, you can find an essay on the subject at Steve Yegge's blog. He is a very experienced programmer, so I give his opinions some credence, but I have never done this myself so I can only point to what others have said.

If you wanted to remain with the more C++ style of classes and instances instead of the less familiar prototypes, look at how Python objects and serialization work. Python also use a "properties" approach to implementing its objects, but the properties are used to implement classes and inheritance instead of a prototype based system, so it may be a little more familiar.

Sorry that I don't have a simpler answer to your question! But hopefully this will help.

情绪少女 2024-08-14 16:01:21

I'm not entirely sure that I understood you intention, however the Qt framework contains a powerful meta object system that lets you do most operation expected from a reflection a system: Getting the class name as string, checking if a object is a instance of a given type, listing and invoking methods, etc.

我很OK 2024-08-14 16:01:21

我使用了 ROOT 的 Reflex 库,效果很好。它不是像您所描述的那样使用疯狂的宏/模板解决方案,而是在构建时处理您的 C++ 头文件以创建反射字典,然后对这些字典进行操作。

I've used ROOT's Reflex library with good results. Rather than using crazy macro / template solutions like you described, it processes your C++ header files at build time to create reflection dictionaries then operates off of those.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文