【问题标题】:Enum values as char array in c++ [duplicate]在c ++中枚举值作为char数组[重复]
【发布时间】:2013-02-22 07:25:33
【问题描述】:

我在 C++ 中有一个枚举

enum color {
    BLACK,GREEN,RED
};

在java中,我可以使用color.BLACK.name()接收字符串值会给我字符串值..

c++ 是否有相同的行为,或者我如何将枚举转换为 char*。

【问题讨论】:

  • 简答无。不过,您可以使用许多“惯用的”宏技巧来模仿这种效果。
  • @sehe 你能解释一下吗?
  • 在 C++ 中,您不会问“如何将 X 转换为 char*”,而是“如何为 X 编写输出流运算符”。使用该运算符,您可以流式传输到字符串、stdout、stderr、文件...

标签: c++ enums


【解决方案1】:

简短回答没有。不过,您可以使用许多“惯用的”宏技巧来模仿这种效果。

原因是 C++ 倾向于让你“只为你需要的东西付费”(如果你精心设计,这使得编写性能更高的代码成为可能)。

代码项目对此有an article,谷歌会发现很多其他的。

【讨论】:

    【解决方案2】:

    不久前,我做了一些技巧,让枚举在 QComboBox 中正确显示,并将枚举和字符串表示形式定义为一个语句

    #pragma once
    #include <boost/unordered_map.hpp>
    
    namespace enumeration
    {
    
       struct enumerator_base : boost::noncopyable
       {
          typedef
             boost::unordered_map<int, std::wstring>
             kv_storage_t;
          typedef
             kv_storage_t::value_type
             kv_type;
          kv_storage_t const & kv() const
          {
             return storage_;
          }
    
          LPCWSTR name(int i) const
          {
             kv_storage_t::const_iterator it = storage_.find(i);
             if(it != storage_.end())
                return it->second.c_str();
             return L"empty";
          }
    
       protected:
          kv_storage_t storage_;
       };
    
       template<class T>
       struct enumerator;
    
       template<class D>
       struct enum_singleton : enumerator_base
       {
          static enumerator_base const & instance()
          {
             static D inst;
             return inst;
          }
       };
    }
    
    #define QENUM_ENTRY(K, V, N)  K, N storage_.insert(std::make_pair((int)K, V));
    
    #define QBEGIN_ENUM(NAME, C)   \
    enum NAME                     \
    {                             \
       C                          \
    }                             \
    };                            \
    }                             \
    
    #define QEND_ENUM(NAME) \
    };                     \
    namespace enumeration  \
    {                      \
    template<>             \
    struct enumerator<NAME>\
       : enum_singleton< enumerator<NAME> >\
    {                      \
       enumerator()        \
       {
    
    //usage
    /*
    QBEGIN_ENUM(test_t,
       QENUM_ENTRY(test_entry_1, L"number uno",
       QENUM_ENTRY(test_entry_2, L"number dos",
       QENUM_ENTRY(test_entry_3, L"number tres",
    QEND_ENUM(test_t)))))
    */
    

    现在enumeration::enum_singleton&lt;your_enum&gt;::instance() 能够将枚举转换为字符串。如果您将kv_storage_t 替换为boost::bimap,您还可以进行反向转换。 引入converter的通用基类将其存储在Qt对象中,因为Qt对象不能是模板

    【讨论】:

      【解决方案3】:

      语言中没有对此的直接支持。事实上,它 对于一般人来说,即使不是不可能,也很难做到 案例:C++ 中的enum 与 枚举类型,你可以这样写:

      enum E
      {
          a = 0,
          b = 0,
          c = 0
      };
      

      ,有几个枚举常量具有相同的值。

      (我已经编写了解析枚举语句并生成 将代码映射到字符串和从字符串映射;给定一个枚举,例如 上面,它将一个枚举值 0 映射到 "a",不管 是否设置为abc。还是很不可思议 有用,但我认为这是正确的解决方案:外部 生成映射的程序如果您需要或请求它们。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-05
        • 1970-01-01
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 2012-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多