C中的设置数组

前端之家收集整理的这篇文章主要介绍了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.f2er.com/c/113106.html

猜你在找的C&C++相关文章