【发布时间】:2014-04-15 20:12:45
【问题描述】:
枚举类型的大小是多少?
int main()
{
enum boolean{fl,tr};
int sbool = sizeof(boolean);
boolean a=fl,b=tr;
cout<<"size of Bool Enum: "<<sbool<<endl;
enum months{jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
int smon = sizeof(months);
cout<<"size of months enum: "<<smon<<endl;
}
我写了上面的代码,我得到了 sbool 和 smon 是 4。谁能解释为什么?
【问题讨论】:
-
因为这是
boolean的大小,并且是您系统上合理的整数值。 -
语言标准中没有指定大小。但是你可以使用scoped enumerations,并且可以为这些指定底层类型。
标签: c++ enumeration