封装私有枚举

Ano*_*ous 4 c++ enums

以前我已经定义了枚举类型,这些类型在类的头文件中是私有的.

private:
  enum foo { a, b, c };
Run Code Online (Sandbox Code Playgroud)

但是,我不想再露出枚举的细节了.在实现中定义枚举类似于定义类不变量吗?

const int ClassA::bar = 3;
enum ClassA::foo { a, b, c };
Run Code Online (Sandbox Code Playgroud)

我想知道这是否正确的语法.

Pav*_*aev 9

C++没有枚举的前向声明,因此您无法将枚举"type"与枚举"implementation"分开.

以下内容可以在C++ 0x中实现:

// foo.h
class foo {
   enum bar : int; // must specify base type
   bar x; // can use the type itself, members still inaccessible
};

// foo.cpp
enum foo::bar : int { baz }; // specify members
Run Code Online (Sandbox Code Playgroud)