【发布时间】:2020-07-30 19:57:27
【问题描述】:
在 C++11 中,我们有 enum 的作用域,我们可以如下使用它。
#include <iostream>
enum class Color
{
RED,
BLUE,
};
int main()
{
Color color = Color::RED;
if (color == Color::RED)
{
std::cout << "red" << std::endl;
}
return 0;
}
我已经在我的项目中到处使用范围枚举。
现在我必须迁移到 C++98,所以不能再使用作用域枚举。
如何在 C++98 中实现一个作用域枚举并像 C++11 中的枚举一样使用?
如果实现技术复杂,我们可以将其提取到模板中吗?
Follow link已经讲了一些技术,但不像C++11那么简单。
例如:
namespace Color
{
enum MyColor
{
RED,
BLUE,
};
}
感谢您的宝贵时间。
【问题讨论】:
-
这能回答你的问题吗? Do we really need "enum class" in C++11?