使用枚举声明? [英] using declaration with enum?

查看:145
本文介绍了使用枚举声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用声明似乎不能使用枚举类型

using declaration does not seem to work with enum type

class Sample{
public:
enum Colour { RED,BLUE,GREEN};
}

using Sample::Colour;

不工作!
我们需要为枚举类型的每个枚举器添加使用声明吗?如下所示

does not work!! do we need to add using declaration for every enumerators of enum type? like below

using sample::Colour::RED;


推荐答案

类不定义命名空间,因此using 不适用于此处。

A class does not define a namespace, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆