struct in union initialize

风格不统一 提交于 2019-12-13 07:58:35

问题


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.


回答1:


The initialisation should be:

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


来源:https://stackoverflow.com/questions/51689166/struct-in-union-initialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!