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一个重载的构造函数,具有不同的行为,这取决于指向成员的类型,你会得到你.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...