带有枚举的“使用"声明 [英] A 'using' declaration with an enum
本文介绍了带有枚举的“使用"声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 声明似乎不适用于枚举类型:
A using declaration does not seem to work with an enum type:
class Sample{
public:
enum Colour {RED, BLUE, GREEN};
}
using Sample::Colour;
不起作用!
我们是否需要为每个枚举类型的枚举符添加一个 using 声明?如下所示:
Do we need to add a using declaration for every enumerators of enum type? Like below:
using sample::Colour::RED;
推荐答案
一个类未定义名称空间,因此使用"表示该名称空间.在这里不适用.
A class does not define a namespace, and therefore "using" isn't applicable here.
此外,您需要公开枚举.
Also, you need to make the enum public.
如果您要在同一类中使用枚举,请参见以下示例:
If you're trying to use the enum within the same class, here's an example:
class Sample {
public:
enum Colour { RED, BLUE, GREEN };
void foo();
}
void Sample::foo() {
Colour foo = RED;
}
并从班级外部访问它:
void bar() {
Sample::Colour colour = Sample::RED;
}
这篇关于带有枚举的“使用"声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文