联合初始化中的struct [英] struct in union initialize

查看:54
本文介绍了联合初始化中的struct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在一个联合体中有一个结构。例如,

Currently, I have a struct in a union. For example,

Struct foo{
    Union u{
         Struct s1{
             int i1;
         } ss1;
         Struct s2{
             int i2;
         } ss2;
    } wrap;
};

因此,当我想初始化联合时,我试图这样做。

So when I want to initialize the union, I tried to do like this.

foo f = {};
f.u.ss1 = {
    .i1 = 0;
}

但错误显示操作符=(操作符类型和大括号括起来的字符不匹配初始化程序列表)。

But the error shows no match for operator = (operand types and braced-enclosed initializer list).

那么进行初始化的正确方法是什么?

So what is the right way to do the initialize? Thanks in advance.

推荐答案

初始化应该是:

foo f;
f.wrap.ss1 = {0 /*, comma seperated values, */};

这篇关于联合初始化中的struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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