封装C++ enum ,并设置enum 的名字

本文介绍了一种使用C++将枚举类型封装到结构体中的方法,并通过宏定义实现了枚举值与字符串名称的映射,方便了枚举类型的使用及调试。
 

/*!
 * \b author: Peng Futian 
 *
 * \b Date: 2011-07-30
 *
 * \b Description: 将enum封装在struct中,并且设置enum的名字
 * 用法:
 *  类型定义,为了与数组中的名字对应,元素值应该用系统缺省的设置,从0开始
 *  DECLARE_MV_ENUM(FileShare, 10)
 *   Read,
 *   Write,
 *  END_DECLARE_MV_ENUM()
 *
 *  实现部分,需要与定义中相应元素位置一一对应
 *  IMPLEMENT_MV_ENUM(E)
 *   "Read",
 *   "Write"
 *  END_IMPLEMENT_MV_ENUM()
 *
 *  @param E 类型名
 *  @param S 元素的个数
 *
 */
#define DECLARE_MV_ENUM(E, S) \
struct E \
{ \
private: \
 static std::string name_list_[];\
public: \
  E(const int type = 0) : type_((Type)type) { \
  } \
  E(const E& rhs) : type_(rhs.type_) { \
  } \
  E& operator=(const E& rhs) { \
    if(this != &rhs) {\
      this->type_ = rhs.type_; \
    } \
    return *this; \
  } \
  E& operator=(const int type) { \
    this->type_ = (Type)type; \
    return *this; \
  } \
  bool operator==(const E& rhs) const { \
    return type_ == rhs.type_; \
  } \
  operator int() const { \
    return this->type_; \
  } \
  const std::string& GetName() const{\
   return name_list_[type_]; \
  } \
  static const int SIZE = S;\
  enum Type \
  {

#define END_DECLARE_MV_ENUM() \
  }; \
  static const std::string& GetName(const Type type) { \
    return name_list_[type]; \
  } \
  bool operator==(const Type& type) const { \
    return type_ == type; \
  } \
  private: \
    Type type_; \
};

#define IMPLEMENT_MV_ENUM(E) \
  std::string E::name_list_[] = \
  {

#define END_IMPLEMENT_MV_ENUM() \
  };

### C++ 枚举 (`enum`) 的使用方法 #### 什么是 `enum`? 在 C++ 中,`enum` 是一种用于定义一组具名常量的数据类型。通过使用枚举,可以使代码更易于阅读和维护,同时提供更强的类型安全性。 --- #### 基本语法 以下是 `enum` 的基本声明方式: ```cpp enum 名称 { 枚举值列表 }; ``` 例如: ```cpp enum Colors { Red, Green, Blue }; ``` 在此例子中,`Red`, `Green`, 和 `Blue` 被赋予默认整数值分别为 `0`, `1`, 和 `2`[^1]。 如果需要自定义初始值,则可以通过显式赋值实现: ```cpp enum Numbers { One = 1, Two = 2, Three = 4 }; ``` 这里,`One` 对应于 `1`,`Two` 对应于 `2`,而 `Three` 则对应于 `4`[^3]。 --- #### 如何使用 `enum`? 下面是一个完整的程序示例来演示如何使用 `enum`: ```cpp #include <iostream> using namespace std; int main() { // 定义一个枚举类型 enum GameResult { WIN, LOSE, TIE }; // 创建一个枚举变量初始化 GameResult result = WIN; // 条件判断 if (result == WIN) { cout << "胜利!" << endl; } else if (result == LOSE) { cout << "失败..." << endl; } else if (result == TIE) { cout << "平局." << endl; } return 0; } ``` 在这个例子中,`GameResult` 表示游戏的结果可能有三种情况:获胜(`WIN`)、失败(`LOSE`) 或 平局(`TIE`)。这些名称比直接使用的数字更具描述性。 --- #### 强类型枚举 `enum class` 为了增强类型安全性和防止命名冲突,C++11 引入了 `enum class`。它的工作原理类似于普通的 `enum`,但是提供了更好的封装特性。 ##### 示例代码: ```cpp #include <iostream> // 使用强类型枚举 enum class Direction { North, South, East, West }; void showDirection(Direction dir) { switch(dir){ case Direction::North: std::cout << "向北"; break; case Direction::South: std::cout << "向南"; break; case Direction::East: std::cout << "向东"; break; case Direction::West: std::cout << "向西"; break; } } int main(){ Direction d = Direction::North; showDirection(d); return 0; } ``` 注意,在访问 `enum class` 成员时必须加上作用域解析符(`.`),这有助于避免不同枚举之间的名字冲突[^2]。 --- #### 错误处理与注意事项 尝试将两个不同的传统 `enum` 类型混用可能会引发编译器警告或错误。然而,当改用 `enum class` 后,这种问题会被完全消除因为它们被设计成独立的作用域。 另外需要注意的是,尽管可以为某些数据结构分配字符作为其成员,但实际上这些字符会自动转换为其 ASCII 数字形式而不是保持原样[^4]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值