微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C中的设置数组

我的目标是设置一个数据结构来存储我的应用程序的设置.

PHP中,我只会写…

$settings = array(
    "Fullscreen" => true,"Width"      => 1680,"Height"     => 1050,"Title"      => "My Application",);

现在我试图在C中创建一个类似的结构,但它不能处理不同的数据类型.顺便说一句,如果有更好的方式存储这些设置数据,请让我知道.

struct Setting{ string Key,Value; };

Setting Settings[] = {
    ("Fullscreen","true"),// it's acceptable to store the boolean as string
    ("Width","1680"),// it's not for integers as I want to use them later
    ("Height",1050),// would be nice but of course an error
    ("Title","My Application") // strings aren't the problem with this implementation
};

如何使用灵活的数据类型来建立关联数组的结构?

解决方法

具有不同数据类型的关联数据结构正是一个结构体…
struct SettingsType
{
    bool Fullscreen;
    int Width;
    int Height;
    std::string Title;
} Settings = { true,1680,1050,"My Application" };

现在,也许你想要一些反思,因为字段名称会出现在配置文件中?就像是:

SettingsSerializer x[] = { { "Fullscreen",&SettingsType::Fullscreen },{ "Width",&SettingsType::Width },{ "Height",&SettingsType::Height },{ "Title",&Settings::Title } };

只要你给SettingsSerializer一个重载的构造函数,具有不同的行为,这取决于指向成员的类型,你会得到你.

原文地址:https://www.jb51.cc/c/113106.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐