C++:动态访问类属性

发布于 2024-09-19 09:13:09 字数 413 浏览 3 评论 0原文

我想知道是否可以在不知道运行时访问的属性名称的情况下动态访问类的属性。

为了让您更好地理解我的目标,这段 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 技术交流群。

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

发布评论

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

评论(3

十六岁半 2024-09-26 09:13:14

是的,这是可能的...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.

仅冇旳回忆 2024-09-26 09:13:13

如果序列化是您主要关心的问题,请查看现有的解决方案,例如 Boost.Serialization 选择流式传输方法:

// save data to archive:
// create and open a character archive for output
std::ofstream ofs("filename");
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << myInstance;

// ...

// restore data from archive:
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> myInstance;

If serialization is your main concern, take a look at existing solutions like Boost.Serialization which opts for a streaming approach:

// save data to archive:
// create and open a character archive for output
std::ofstream ofs("filename");
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << myInstance;

// ...

// restore data from archive:
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> myInstance;
岁月静好 2024-09-26 09:13:12

基本上,您需要创建一个额外的函数,ala:

std::string get(const std::string& field, const std::string& value_representation)
{
    std::ostringstream oss;
    if (field == "a")
        oss << a;
    else if (field == "b")
        oss << b;
    else
        throw Not_Happy("whada ya want");
    return oss.str();
}

有很多现有的框架可以做到这一点,例如 boost 序列化库。大多数涉及使用某种标记来声明您的类,或者更糟糕的是使用非本地化的辅助元数据。

如果您确实需要侵入性较小的东西,那么 Gcc-XML 和 OpenC++ 等工具可以让您自动生成这些函数(前者可以轻松地与 XML 库、一点 python 或 ruby​​ 结合使用,以获得简单但性能较低的胜利)。

需要明确的是,C++ 不提供任何自动化的、符合标准的方法来执行此操作。没有流行的编译器为此提供扩展。绝望中,您也许可以在某个地方解析您自己的调试信息,但这绝对不推荐。

Basically, you need to create an extra function, ala:

std::string get(const std::string& field, const std::string& value_representation)
{
    std::ostringstream oss;
    if (field == "a")
        oss << a;
    else if (field == "b")
        oss << b;
    else
        throw Not_Happy("whada ya want");
    return oss.str();
}

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.

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