C++ enum class


enum class NewColor { Red, Green = 20,Blue };

void testNewEnum()
{
	NewColor r = NewColor::Blue;
	switch (r)
	{
	case NewColor::Red:
		break;
	case NewColor::Green:
		break;
	case NewColor::Blue:
		break;
	default:
		break;
	}
	//需要明确的转换
	int a = static_cast<int>(r);

}

 //根据类型 来确定enum的大小
 //取值范围在short之内,不然编译会出错
//默认是int 类型
enum class NewColor2 :short { Red,Green=20,Blue };

void testNewEnum2()
{
	NewColor2 r = NewColor2::Blue;
	switch (r)
	{
	case NewColor2::Red:
		break;
	case NewColor2::Green:
		break;
	case NewColor2::Blue:
		break;
	default:
		break;
	}

}

enum class IsGood
{
	Yes,
	No
};

void show(IsGood)
{

}

int main()
{
	show(IsGood::Yes);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37981386/article/details/85953509
今日推荐