这是一个C ++面试测试问题而不是作业。
#include <iostream>
using namespace std;
enum months_t { january, february, march, april, may, june, july, august, september,
october, november, december} y2k;
int main ()
{
cout << "sizeof months_t is " << sizeof(months_t) << endl;
cout << "sizeof y2k is " << sizeof(y2k) << endl;
enum months_t1 { january, february, march, april, may, june, july, august,
september, october, november, december} y2k1;
cout << "sizeof months_t1 is " << sizeof(months_t1) << endl;
cout << "sizeof y2k1 is " << sizeof(y2k1) << endl;
}
输出:
sizeof months_t是4
sizeof
y2k是4
sizeof months_t1是4 sizeof y2k1是4
为什么所有这4个字节的大小都是?不是12 x 4 = 48个字节?
我知道联合元素占据相同的内存位置,但这是一个枚举。