详细解读C++编程中的匿名类类型和位域
匿名类类型 typedef struct { unsigned x; unsigned y; } POINT; 注意 struct PTValue { POINT ptLoc; union { int iValue; long lValue; }; }; PTValue ptv; 在上面的代码中,可以使用对象成员选定内容运算符 (iValue.) 访问 ,如下所示: int i = ptv.iValue; 匿名类受某些限制的约束。(有关匿名联合的详细信息,请参阅联合。) 匿名类: C++ 位域 declarator : constant-expression 备注 // bit_fields1.cpp // compile with: /LD struct Date { unsigned short nWeekDay : 3; // 0..7 (3 bits) unsigned short nMonthDay : 6; // 0..31 (6 bits) unsigned short nMonth : 5; // 0..12 (5 bits) unsigned short nYear : 8; // 0..100 (8 bits) }; Date 类型的对象的概念上的内存布局如下图所示。 数据对象的内容布局 // bit_fields2.cpp // compile with: /LD struct Date { unsigned nWeekDay : 3; // 0..7 (3 bits) unsigned nMonthDay : 6; // 0..31 (6 bits) unsigned : 0; // Force alignment to next boundary. unsigned nMonth : 5; // 0..12 (5 bits) unsigned nYear : 8; // 0..100 (8 bits) }; 则内存布局如下图中所示。 带有零长度位域的数据对象的布局 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |