如何在C++中实现带多表示形式的作用域枚举?
在C++中实现带多表示的枚举类(类似Java枚举)
你想要在C中实现一种类似Java枚举的类型,每个枚举值同时拥有唯一的数值(numericCode)和字符串(alphaCode)表示,并且通过私有构造函数防止外部新增枚举值,以此消除代码中的硬编码魔术数,典型场景比如ISO 3166国家编码。你已经在Java中实现过该功能,并给出了Java示例和自己构想的C伪代码,想知道C++中如何实现。
你提供的Java实现示例
public enum Foo { Value01 ( 001, "aa" ), Value02 ( 002, "bb" ), ValueNN ( 999, "zz" ); private final int numericCode; private final String alphaCode; Foo( final int numeric, final String alpha ) { numericCode = numeric; alphaCode = alpha; } public String getAlpha() { return alphaCode; } public int getNumeric() { return numericCode; } }
你构想的C++伪代码
enum class Foo { Value01 { 001, "aa" }, Value02 { 002, "bb" }, ..... ValueNN { nn, "zz" }; private: Foo( int numeric, const string alpha ) : numericCode( numeric ), alphaCode( alpha ) {} private: int numericCode; string alphaCode; };
C++中的实现方案
C++原生的enum class不支持为枚举值添加成员变量或自定义构造函数,所以需要用类+静态常量实例的方式模拟Java枚举的行为,完全满足你的需求:
完整实现代码
#include <string> #include <vector> #include <stdexcept> class Foo { public: // 禁用拷贝和移动,确保实例唯一 Foo(const Foo&) = delete; Foo& operator=(const Foo&) = delete; Foo(Foo&&) = delete; Foo& operator=(Foo&&) = delete; // 获取枚举值的字符串和数值表示 const std::string& getAlphaCode() const { return alphaCode_; } int getNumericCode() const { return numericCode_; } // 静态枚举实例,对应Java中的枚举值 static const Foo Value01; static const Foo Value02; static const Foo ValueNN; // 可选:通过数值查找对应的枚举实例 static const Foo& fromNumeric(int numeric) { for (const auto& foo : getAllValues()) { if (foo.getNumericCode() == numeric) { return foo; } } throw std::invalid_argument("Invalid numeric code"); } // 可选:通过字符串查找对应的枚举实例 static const Foo& fromAlpha(const std::string& alpha) { for (const auto& foo : getAllValues()) { if (foo.getAlphaCode() == alpha) { return foo; } } throw std::invalid_argument("Invalid alpha code"); } // 可选:获取所有枚举值列表 static const std::vector<Foo*>& getAllValues() { static const std::vector<Foo*> values = {&Value01, &Value02, &ValueNN}; return values; } private: // 私有构造函数,外部无法创建新实例 Foo(int numericCode, const std::string& alphaCode) : numericCode_(numericCode), alphaCode_(alphaCode) {} int numericCode_; std::string alphaCode_; }; // 在类外部初始化静态枚举实例 const Foo Foo::Value01(1, "aa"); const Foo Foo::Value02(2, "bb"); const Foo Foo::ValueNN(999, "zz");
实现说明
- 私有构造函数:确保只有类内部能创建实例,外部无法新增枚举值,完全符合你的需求。
- 静态常量实例:对应Java枚举中的每个枚举值,全局唯一且不可修改。
- 禁用拷贝/移动:防止实例被复制,保证每个枚举值只有一个实例。
- 访问方法:
getAlphaCode()和getNumericCode()提供对两种表示的只读访问。 - 查找方法:可选实现
fromNumeric和fromAlpha,方便从数值或字符串反向查找枚举实例,避免硬编码判断。 - 枚举列表:
getAllValues()可以遍历所有枚举值,适合需要批量处理的场景。
使用示例
#include <iostream> int main() { // 直接使用枚举实例 std::cout << Foo::Value01.getAlphaCode() << ": " << Foo::Value01.getNumericCode() << std::endl; // 通过数值查找 try { const Foo& foo = Foo::fromNumeric(999); std::cout << "Found: " << foo.getAlphaCode() << std::endl; } catch (const std::invalid_argument& e) { std::cerr << e.what() << std::endl; } return 0; }
内容的提问来源于stack exchange,提问作者Kevin Hannan
相关产品推荐
相关产品推荐

