C++:动态访问类属性
我想知道是否可以在不知道运行时访问的属性名称的情况下动态访问类的属性。
为了让您更好地理解我的目标,这段 php 代码应该演示我想要做的事情:
<?php
$object = new object();
$property = get_name_out_of_textfile();
echo $object->$property;
?>
我知道不可能做完全相同的事情,但假设我在 a 中获得了属性的名称std::string 的 cstring 并想要访问与名称匹配的属性。有办法做到这一点吗?
背景是我想从一个具有很多属性的类中动态加载和保存数据。我可以将其保存为我想要的任何格式,但我需要一种方法来再次加载它们,而无需每次都指定确切的属性。
预先感谢您的任何帮助, 罗宾.
I'm wondering if it is possible to access a property of a class dynamically without knowing the name of the property accessed at run-time.
To give you a better understanding of what I'm aiming at, this php code should demonstrate what I want to do:
<?php
$object = new object();
$property = get_name_out_of_textfile();
echo $object->$property;
?>
I know that is it not possible to do the exact same thing but let's say I got the name of an attribute in a cstring of std::string and want to access the attribute matching the name. Is there a way to do that?
The background is that I want to dynamically load and save data from a class with lots of attributes. I can save the in any format I want but I need a way to load them back in again without specifying the exact attribute every time.
Thanks in advance for any help,
Robin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是可能的...Python、php 等...是在 C 或 C++ 之上实现的,并且它们提供了它。
但这也非常尴尬。
您所要求的,在运行时检查类型的能力,称为内省。这很难实现,所以考虑到你提出问题的方式......还不要考虑它,你需要更多的经验。
如果你想反省,真的可以考虑另一种语言。
Yes it's possible... Python, php, etc... are implemented on top of C or C++, and they offer it.
But it's also extremely awkward.
What you are asking for, the ability to inspect a type at runtime, is called introspection. It is difficult to implement, so given the way you phrased your question... don't think about it yet, you need more experience.
If you want introspection, think about another language, really.
如果序列化是您主要关心的问题,请查看现有的解决方案,例如 Boost.Serialization 选择流式传输方法:
If serialization is your main concern, take a look at existing solutions like Boost.Serialization which opts for a streaming approach:
基本上,您需要创建一个额外的函数,ala:
有很多现有的框架可以做到这一点,例如 boost 序列化库。大多数涉及使用某种标记来声明您的类,或者更糟糕的是使用非本地化的辅助元数据。
如果您确实需要侵入性较小的东西,那么 Gcc-XML 和 OpenC++ 等工具可以让您自动生成这些函数(前者可以轻松地与 XML 库、一点 python 或 ruby 结合使用,以获得简单但性能较低的胜利)。
需要明确的是,C++ 不提供任何自动化的、符合标准的方法来执行此操作。没有流行的编译器为此提供扩展。绝望中,您也许可以在某个地方解析您自己的调试信息,但这绝对不推荐。
Basically, you need to create an extra function, ala:
There are lots of existing frameworks to do this, such as the boost serialization library. Most involve declaring your class using some kind of markup, or worse - delocalised secondary metadata.
If you really need something less invasive, then tools such as Gcc-XML and OpenC++ allow you to automate the generation of these functions (the former can easily be combined with XML libraries, a little python or ruby, for an easy but low performance win).
Just to be absolutely clear, C++ does not provide any automated, Standard-compliant way to do this. No popular compilers provide extensions for this. In desparation, you may be able to get somewhere parsing your own debug information, but that's definitely not recommended.