位域(bit-field):一种压缩空间的成员
这两天在看公司里关于MPEG-2 码流结构的代码,发现“师傅”写关于那些结构的代码的时候出现了如下类似的代码(这里的载自C++ Prime)。
typedef unsigned int Bit;
class File {
public:
Bit mode
: 2;
Bit modified
: 1;
Bit prot_owner
: 3;
Bit prot_group
: 3;
Bit prot_world
: 3;
// ...
};
刚开始觉得有些怪怪的,虽然从结构中大概知道是什么意思,但是具体的却不怎么知道。可能是C/C++的知识有些遗漏,实践比较少吧。于是查找资料,得知:
这是一种被称为位域bit-field 的