Union declaration
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
本章尚未完成 原因:Expand description and example |
工会是一类特殊类型的存储的所有数据成员在相同的内存位置.
原文:
A union is a special class type that stores all of its data members in the same memory location.
工会不能有虚函数,继承或继承其他类.
原文:
Unions cannot have virtual functions, be inherited or inherit other classes.
(C++11 起)如果一个union中包含一个非POD的成员,其中有一个用户定义的特殊功能,该功能是默认情况下,在工会中删除,并需要明确定义的用户(构造函数,析构函数,拷贝构造函数或复制赋值).
原文:
(C++11 起) If a union contains a non-POD member, which has a user-defined special function (constructor, destructor, copy constructor or copy assignment) that function is deleted by default in the union and needs to be defined explicitly by the user.
目录 |
[编辑] 语法
union name { member_declarations } object_list (可选) ;
|
(1) | ||||||||
union { member_declarations } ;
|
(2) | ||||||||
[编辑] 解释
#命名的工会
#无名氏工会
[编辑] 匿名工会
一个匿名的工会成员从封闭的范围作为单一变量.
原文:
Members of an anonymous union are accessible from the enclosing scope as single variables.
匿名工会有进一步的限制:他们必须有唯一的公共成员,不能有成员函数.
原文:
Anonymous unions have further restrictions: they must have only public members and cannot have member functions.
命名空间的匿名工会必须是静态的.....
原文:
Namespace-scope anonymous unions must be static.
[编辑] 示例
输出:
as int: 1024 as char: 128
(little-endian处理器)