Struct初始化中的结构

时间:2017-01-03 16:14:34

标签: c++ struct unions

有人可以解释为什么pp1编译,但pp2没有?

我想它与联合有关,编译器会尝试将{}之间的所有东西都放到buffer8而不是DATA中吗?

#include <iostream>
using namespace std;


union PassFilterValues
{
    struct DATA 
    {
        int32_t a;
        int32_t b;
        int32_t c;
        int32_t k;
    } data;
    uint8_t buffer8[sizeof(DATA)];  
};

union PassFilterDynamic
{
    struct DATA
    {
        PassFilterValues lowFilter;
        PassFilterValues highFilter;
    } data; 
    uint8_t buffer8[sizeof(DATA)];
};

#define PASSFILTER_OFF                  {0,1,2,3}

int main() {
    PassFilterDynamic pp1 = {((PassFilterValues)PASSFILTER_OFF), ((PassFilterValues)PASSFILTER_OFF)};
    PassFilterDynamic pp2 = {PASSFILTER_OFF, PASSFILTER_OFF};
    return 0;
}

随时可以尝试:http://ideone.com/XYhfIi

0 个答案:

没有答案