c++ - 封装私有(private)枚举

标签 c++ enums

之前我已经在类的头文件中定义了旨在私有(private)的枚举类型。

private:
  enum foo { a, b, c };

但是,我不想再公开枚举的详细信息。在实现中定义枚举是否类似于定义类不变量?

const int ClassA::bar = 3;
enum ClassA::foo { a, b, c };

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

最佳答案

C++ 没有枚举的前向声明,因此您不能将枚举“类型”与枚举“实现”分开。

在 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

关于c++ - 封装私有(private)枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1837159/

相关文章:

c++ - 从 C++ 中的模板类实现我自己的数组

c - 为什么会出现这个段错误?

c# - 如何用#字符写枚举?

scala - 如何在 Scala 中构建合适的命名空间?

java - 如何使用 ENUM 从三个中返回随机字符串?

rust - 如何在Rust中将ref转换为en16的枚举值(基本枚举类型)?

c++ - 嵌套的 boost::assign:list_of 在 Visual Studio 2012 中损坏 - 对重载函数的调用不明确

c++ - 是否需要释放/释放 GL_TEXTURE?

c++ - 使用 std::map 部分匹配键

c++ - C++:递归锁-有什么缺点吗?