【发布时间】:2017-12-07 16:57:44
【问题描述】:
这可能是一个有点奇怪的问题,但我真的不知道如何更好地表达它。
我刚刚发现我可以做到以下几点:
#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;
}
所以现在我在 Colour 类型的变量中有整数值 15。
这里到底发生了什么?那是某种枚举“构造函数”吗?据我所知,整数值 15 没有放入枚举中,它只是存储在变量c 中。首先,为什么这样的东西会有用——创建一个枚举中不存在的值?
【问题讨论】:
-
这只是编写 C 风格演员表
(Colour)15的另一种方式。
标签: c++ c++11 enums enum-class