我如何声明一个结构的变量,该变量位于联合中,而该联合位于另一个结构中

How can i declare a variable of a struct which is in a union and this union is in another struct

本文关键字:结构 变量 另一个 于联 何声明 声明 一个      更新时间:2023-10-16

示例代码

struct A
{
 union
  {
    struct B
    {
     short a:1;
     short b:1;
    }; 
  };

 };

我想将一个变量声明为结构 B 的变量。我该怎么做?

该标准不允许在匿名union中使用嵌套类型。你的代码是不合法的。

从C++标准草案N3337:

9.5 工会

5 形式的并集

union { 成员规范 } ;

称为匿名联合;它定义未命名类型的未命名对象。匿名联合的成员规范应仅定义非静态数据成员。[ 注意:嵌套类型和函数不能 在匿名工会内宣布。— 尾注 ]