c++ - 匿名 union 和普通 union

标签 c++ c struct unions

任何人都可以提及普通和匿名 union (或结构)之间的区别吗? 我刚找到一个:
不能在匿名 union 中定义函数。

最佳答案

您不需要点运算符“.”访问匿名 union 元素。

#include <iostream> 
using namespace std;
int main() {
   union {
      int d;
      char *f;
   };

   d = 4;
   cout << d << endl;

   f = "inside of union";
   cout << f << endl;
}

在这种情况下,这将成功编译,但对于普通 union ,“否”。

另外,Anonymous union 只能有 public 成员。

PS :Simply omitting the class-name portion of the syntax does not make a union an anonymous union. For a union to qualify as an anonymous union, the declaration must not declare an object.

关于c++ - 匿名 union 和普通 union ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122138/

相关文章:

C++ 在嵌入式系统中需要汇编

c++ - 使用-(存档-)gcc命令

C: 为什么for循环中计数器没有重置为0

c - x86 程序集中出现奇怪的段错误

swift - 如何修复 viewDIdLoad 中的 indexPath 错误?

c++ - 将字符串发送到 Arduino 的最佳方式?

c++ - AMD_pinned_memory - 同步传输到纹理

c - 运行的进程不遵循系统时间

c - 总线错误 : 10 in C dealing with struct pointers

c - 如何仅使用结构中的几个结构成员在 c 中创建新的打包结构?