对象反射 [英] Object Reflection

查看:177
本文介绍了对象反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何参考建立一个完整的对象/类反射系统在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.

谢谢!

推荐答案

使用模板和宏自动或半自动定义一切都是唯一选项。 C ++具有非常弱的反射/内省能力。然而,如果你想做的是主要是序列化和存储,这已经在 Boost序列化库。您可以通过实施 serializer method ,或者有一个外部函数如果你不想修改类。

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.

这似乎不是你问的虽然。我猜想你想要的东西像自动序列化,这需要在类实现者的部分没有额外的努力。他们在 Python Java 和许多其他语言,但不是C ++。为了得到你想要的,你需要实现你自己的对象系统,像是可能的,IgKh在他的答案

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.

如果你想这样做,我建议看看JavaScript如何实现对象。 JavaScript使用基于原型的对象系统,它相当简单,但功能相当强大。我推荐这个,因为它似乎在我喜欢它会更容易实现,如果你必须自己做。如果你正在阅读一篇关于原型的好处和优雅的长期解释,你可以找到关于这个主题的文章在Steve Yegge的博客。他是一个非常有经验的程序员,所以我给他的意见一些信任,但我从来没有这样做,所以我只能指出别人说的话。

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.

如果你希望保留更多的C ++风格的类和实例,而不是不那么熟悉的原型,看看Python 对象序列化工作。 Python还使用属性方法来实现它的对象,但是属性用于实现类和继承而不是基于原型的系统,所以它可能更熟悉一些。

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.

这篇关于对象反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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