如何在使用 g++ 编译器时使用 c 样式初始化结构? [英] How to initialize a struct using the c style while using the g++ compiler?

查看:35
本文介绍了如何在使用 g++ 编译器时使用 c 样式初始化结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,为了在c中初始化一个struct,我们只能指定部分字段.如下图:

Generally, in order to initialize a struct in c, we could only specify part of the fields. Like below:

static struct fuse_operations hello_oper = {
    .getattr    = hello_getattr,
    .readdir    = hello_readdir,
    .open       = hello_open,
    .read       = hello_read,
};

但是,在 C++ 中,我们应该在不命名字段的情况下初始化 struct 中的变量.现在,如果我想在使用 g++ 编译器的同时使用 c 样式初始化一个 struct 怎么办?PS:我需要这样做的原因是 struct fuse_operations 中的字段太多.

However, in C++, we should initialize the variables in the struct without naming the fields. Now, what if I would like to initialize a struct using the c style while using the g++ compiler, how to accomplish this? PS: the reason I need to do this is that the struct fuse_operations has too many fields in it.

推荐答案

不幸的是,即使是 C++ 标准的 C++11 版本 缺少 C99 的指定初始化器功能.

Unfortunately, even the C++11 version of the C++ standard lacks the designated initializers feature of C99.

这篇关于如何在使用 g++ 编译器时使用 c 样式初始化结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆