这可能是一个有点奇怪的问题,但我真的不知道如何更好地表达它。
我刚刚发现我可以执行以下操作:
#include <iostream>
enum class Colour // also works with plain old enum
{
Red = 1,
Green,
Blue,
Yellow,
Black,
White
};
int main()
{
Colour c = Colour(15); // this is the line I don't quite understand
std::cout << static_cast<int>(c) << std::endl; // this returns 15
return 0;
}
所以现在我在类型的变量中有整数值 15 Colour
。
这里到底发生了什么?那是某种枚举“构造函数”吗?据我所知,整数值 15 没有放入枚举中,它仅存储在 variable 中c
。首先,为什么这样的东西会有用——创建一个枚举中不存在的值?