《c++程序设计语言(第四版)》8.4章节
1、枚举分成两种enum class和enum,前者枚举值不会隐式转换成其他类型,后者值会转成整数。一般应该使用前者。
2、枚举值类型一般是int,如果认为int类型浪费空间也可以显示地指定其他类型:
enum class color:char
{
red,
green,
yellow
};
3、枚举的位运算:
enum class color:char
{
red = 5,
green = 6,
yellow = 7
};
constexpr color operator |(color c1,color c2)
{
return static_cast<color>(static_cast<int>(c1) | static_cast<int>(c2));
}
#define debug qDebug()<<
int main(int argc, char *argv[])
{
color c1 = color::red;
color c2 = color::green;
color c3 = c1 | c2;
debug static_cast<int>(c3) << (c3 == color::yellow);// 5 | 6 = 7
}
4、枚举的sizeof()的值等于枚举值类型的(int \ char)sizeof()的值。
5、如果枚举不必用来声明对象,可以使用匿名枚举:
enum color:int
{
red = 666,
green = 999,
yellow = 444
};
#define debug qDebug()<<
int main(int argc, char *argv[])
{
int c = red;
debug c;
}