加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C中的设置数组

发布时间:2020-12-16 06:00:14 所属栏目:百科 来源:网络整理
导读:我的目标是设置一个数据结构来存储我的应用程序的设置. 在PHP中,我只会写… $settings = array( "Fullscreen" = true,"Width" = 1680,"Height" = 1050,"Title" = "My Application",); 现在我试图在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一个重载的构造函数,具有不同的行为,这取决于指向成员的类型,你会得到你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读