如何访问未命名的“枚举类”封装在类里面? [英] How to access unnamed "enum class" encapsulated inside a class?

查看:149
本文介绍了如何访问未命名的“枚举类”封装在类里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class A {
  public: enum class { HELLO, WORLD };
};

知道,在声明一个简单的枚举(而不是枚举类)是一个更好的主意,因为它已经输入 class 识别。但仍然上面的语句是一个有效的 C ++ 0x 签名。现在如何访问一个未命名的枚举类 outside?

Having known that, inside a class, declaring a simple enum (rather than enum class) is a better idea, because it's already typed with the class identification. But still above statement is a valid C++0x signature. Now how to access an unnamed enum class outside ?

int i = A::HELLO; // error: ‘HELLO’ is not a member of ‘A’


推荐答案

其实,这是无效的。 C ++ 0x FDIS说(9.2p1)

Actually, that is not valid. The C++0x FDIS says (9.2p1)


除非用于声明好友(11.3)或介绍成员的名字的基类转换为派生类(7.3.3),成员声明声明类的成员,每个成员声明应至少声明该类的一个成员名称。

Except when used to declare friends (11.3) or to introduce the name of a member of a base class into a derived class (7.3.3), member-declarations declare members of the class, and each such member-declaration shall declare at least one member name of the class.

在您的情况下,类范围中不会引入枚举器名称,也不会引入枚举名称。因此,成员声明不会引入任何成员名称。

In your case, no enumerator name is introduced into the class' scope and no enumeration name is introduced either. So, no member name at all is introduced by that member-declaration.

编辑:实际上,更直接地禁止枚举声明。 7.2p2:

And actually, there's a more direct prohibition of the enumeration declaration. 7.2p2:


在范围枚举的声明中不应省略可选的标识符

这篇关于如何访问未命名的“枚举类”封装在类里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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