简短版本是:如何学习c ++字段的单个字段的大小(以位为单位)?
澄清一下,我所谈论的领域的一个例子:
struct Test {
unsigned field1 : 4; // takes up 4 bits
unsigned field2 : 8; // 8 bits
unsigned field3 : 1; // 1 bit
unsigned field4 : 3; // 3 bits
unsigned field5 : 16; // 16 more to make it a 32 bit struct
int normal_member; // normal struct variable member, 4 bytes on my system
};
Test t;
t.field1 = 1;
t.field2 = 5;
// etc.
Run Code Online (Sandbox Code Playgroud)
我们只是说,要获得整个Test对象的大小很简单
sizeof(Test); // returns 8, for 8 …Run Code Online (Sandbox Code Playgroud)