How to access unnamed “enum class” encapsulated in

2019-06-21 04:54发布

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

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’

1条回答
你好瞎i
2楼-- · 2019-06-21 05:33

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

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.

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

The optional identifier shall not be omitted in the declaration of a scoped enumeration.

查看更多
登录 后发表回答