The Wayback Machine - https://web.archive.org/web/20130629053713/http://zh.cppreference.com:80/w/cpp/language/union

Union declaration

来自cppreference.com

 
 
C + +语言
大会的主题
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
流量控制
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件执行语句
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
迭代语句
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
跳转语句
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
函数声明
lambda函数的声明
函数模板
的历史。内嵌说明
异常规范 (过时了)
noexcept说明 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空间
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
联合类型
函数类型
decltype specifier (C++11)
规范
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV符
存储时间说明符
constexpr说明 (C++11)
汽车符 (C++11)
alignas说明 (C++11)
初始化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
布尔文字
nullptr (C++11)
用户定义的 (C++11)
表达式
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
另一种表示形式
实用工具
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
声明类型别名 (C++11)
属性 (C++11)
施放
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
隐式转换
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-风格和功能转换
内存分配
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类特定的功能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
虚函数
覆盖说明 (C++11)
最后说明 (C++11)
明确的 (C++11)
静态的
特殊的成员函数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
模板
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类模板
函数模板
模板特化
参数包 (C++11)
杂项
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
内联汇编
 
工会是一类特殊类型的存储的所有数据成员在相同的内存位置.
Original:
A union is a special class type that stores all of its data members in the same memory location.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
工会不能有虚函数,继承或继承其他类.
Original:
Unions cannot have virtual functions, be inherited or inherit other classes.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(至 C++11),工会只能包含POD(普通的旧数据)类型.
Original:
(至 C++11) Unions can only contain POD(普通的旧数据) types.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11 起)如果一个union中包含一个非POD的成员,其中有一个用户定义的特殊功能,该功能是默认情况下,在工会中删除,并需要明确定义的用户(构造函数,析构函数,拷贝构造函数或复制赋值).
Original:
(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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 语法

union name { member_declarations } object_list (可选的) ; (1)
union { member_declarations } ; (2)

[编辑] 解释

#命名的工会
Original:
#Named union
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#无名氏工会
Original:
#Anonymous union
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 匿名工会

一个匿名的工会成员从封闭的范围作为单一变量.
Original:
Members of an anonymous union are accessible from the enclosing scope as single variables.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
匿名工会有进一步的限制:他们必须有唯一的公共成员,不能有成员函数.
Original:
Anonymous unions have further restrictions: they must have only public members and cannot have member functions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空间的匿名工会必须是静态的.....
Original:
Namespace-scope anonymous unions must be static.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 为例

union foo
{
  int x;
  signed char y;
};
 
int main()
{
  foo.x = 128 + 896;
  std::cout << "as int: "  << (int)foo.x << '\n';
  std::cout << "as char: " << (int)foo.y << '\n';
  return 0;
}

Output:

as int: 1024
as char: 128
(little-endian处理器)
Original:
(for little-endian processors)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.