任何人都可以提及普通和匿名 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/