在 C++ 中通过 '::' 访问枚举值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10090949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing to enum values by '::' in C++
提问by Jury
I have class like following:
我有如下课程:
class Car
{
public:
Car();
// Some functions and members and <b>enums</b>
enum Color
{
Red,
Blue,
Black
};
Color getColor();
void setColor(Color);
private:
Color myColor;
}
I want to:
我想要:
- access to
Color
values asColor::Red
. It is really hardly to understand code whenCar::Red
is used, when class have a lot enums, subclasses etc. - use type
Color
as function argument or return value - use variable type
Color
inswitch
- 访问
Color
值作为Color::Red
. 当Car::Red
使用时,当类有很多枚举,子类等时,很难理解代码。 - 使用类型
Color
作为函数参数或返回值 - 使用可变型
Color
在switch
I know 3 partial solutions:
我知道 3 个部分解决方案:
- Using embedded class
Color
and enum in it - Using embedded namespace
Color
and enum in it - Using
enum class
- 在其中使用嵌入式类
Color
和枚举 - 在其中使用嵌入式命名空间
Color
和枚举 - 使用
enum class
1 and 2 solutions solves a Color::Red
accession problem, but I can't use functions like Color getColor()
and void setColor(Color)
.
1 和 2 解决方案解决了Color::Red
加入问题,但我不能使用Color getColor()
和这样的函数void setColor(Color)
。
3 solution has a problem: VS2010 doen't support enum class
. GCC v.4.1.2 doesn't support it too. I don't know about later versions of gcc.
3 解决方法有问题:VS2010不支持enum class
. GCC v.4.1.2 也不支持它。我不知道 gcc 的更高版本。
Yes, I'm working on cross-platform project.
I have found thissolution, but it seems ... heavy.
I hope somebody can help me here :)
是的,我正在从事跨平台项目。
我找到了这个解决方案,但它似乎......很重。
我希望有人能在这里帮助我:)
回答by Sebastian Mach
In current C++ (i.e. C++11 and beyond), you canalready access enum values like that:
在当前的C ++(即C ++ 11或更高),你可以已经访问枚举值这样的:
enum Color { Red };
Color c = Color::Red;
Color d = Red;
You can go further and enforce the use of this notation:
您可以更进一步并强制使用此符号:
enum class Color { Red };
Color c = Color::Red;
// Color d = Red; <-- error now
And on a sidenote, you now define the underlying type, which was previously only possible with hacky code (FORCEDWORD
or so anyone?):
在旁注中,您现在定义了底层类型,以前只能使用 hacky 代码(FORCEDWORD
或者任何人?):
enum class Color : char { Red };
回答by Mark B
Name the enum inside the nested class (as example one):
命名嵌套类中的枚举(作为示例一):
class Car
{
public:
struct Color
{
enum Type
{
Red,
Blue,
Black
};
};
Color::Type getColor();
void setColor(Color::Type);
};
回答by obmarg
When I want to do something like this I tend to use a namespace and a typedef outside of th namespace (though usually I'm doing this globally rather than inside a class). Something like this:
当我想做这样的事情时,我倾向于在命名空间之外使用命名空间和 typedef(尽管通常我是在全局而不是在类内部这样做)。像这样的东西:
namespace colors
{
enum Color
{
Red,
Blue
...
}
}
typedef colors::Color Color;
This way you use the namespace to get at the actual colors, but the Color
type itself is still globally accessible:
通过这种方式,您可以使用命名空间来获取实际颜色,但Color
类型本身仍然可以全局访问:
Color myFav = colors::Red;