gpt4 book ai didi

c++ - 匿名 union 和普通 union

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:43 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com