我有一个有两个值的枚举类,我想创建一个接收值的方法 然后返回另一个。我还想维护类型安全(这就是为什么我使用枚举类而不是enum)。

http://www.cplusplus.com/doc/tutorial/other_data_types/没有提到任何关于方法的内容 然而,在我的印象中,任何类型的类都可以有方法。


当前回答

正如在另一个答案中提到的,没有。甚至枚举类也不是一个类。


通常需要枚举方法的原因是,它不是一个常规的(只是递增的)枚举,而是一种按位定义要掩盖的值或需要其他位算术操作:

enum class Flags : unsigned char {
    Flag1 = 0x01 , // Bit #0
    Flag2 = 0x02 , // Bit #1
    Flag3 = 0x04 , // Bit #3
    // aso ...
}

// Sets both lower bits
unsigned char flags = (unsigned char)(Flags::Flag1 | Flags::Flag2);

// Set Flag3
flags |= Flags::Flag3;

// Reset Flag2
flags &= ~Flags::Flag2;

显然,人们会考虑封装必要的操作来重新/设置单个/组位,例如位掩码值,甚至位索引驱动的操作对于操作这样一组“标志”是有用的。

c++11结构/类规范只是支持更好的枚举值访问范围。不多不少!

摆脱不能为枚举(类)声明方法的限制的方法是,使用std::bitset(包装类)或位域联合。

联合,这样的位域联合可以有方法(请看这里的限制!)。

我有一个示例,如何将位掩码值(如上所示)转换为相应的位索引,可以沿着std::bitset在这里使用:BitIndexConverter.hpp 我发现这对于提高一些基于“旗帜”决策的算法的可读性非常有用。

其他回答

它可能不能满足您的所有需求,但使用非成员操作符仍然可以获得很多乐趣。例如:

#include <iostream>

enum class security_level
{
    none, low, medium, high
};

static bool operator!(security_level s) { return s == security_level::none; }

static security_level& operator++(security_level& s)
{
    switch(s)
    {
        case security_level::none: s = security_level::low; break;
        case security_level::low: s = security_level::medium; break;
        case security_level::medium: s = security_level::high; break;
        case security_level::high: break;
    }
    return s;
}

static std::ostream & operator<<(std::ostream &o, security_level s)
{
    switch(s)
    {
        case security_level::none: return o << "none";
        case security_level::low: return o << "low";
        case security_level::medium: return o << "medium";
        case security_level::high: return o << "high";
    }
}

这允许如下代码

security_level l = security_level::none;   
if(!!l) { std::cout << "has a security level: " << l << std::endl; } // not reached
++++l;
if(!!l) { std::cout << "has a security level: " << l << std::endl; } // reached: "medium"

正如在另一个答案中提到的,没有。甚至枚举类也不是一个类。


通常需要枚举方法的原因是,它不是一个常规的(只是递增的)枚举,而是一种按位定义要掩盖的值或需要其他位算术操作:

enum class Flags : unsigned char {
    Flag1 = 0x01 , // Bit #0
    Flag2 = 0x02 , // Bit #1
    Flag3 = 0x04 , // Bit #3
    // aso ...
}

// Sets both lower bits
unsigned char flags = (unsigned char)(Flags::Flag1 | Flags::Flag2);

// Set Flag3
flags |= Flags::Flag3;

// Reset Flag2
flags &= ~Flags::Flag2;

显然,人们会考虑封装必要的操作来重新/设置单个/组位,例如位掩码值,甚至位索引驱动的操作对于操作这样一组“标志”是有用的。

c++11结构/类规范只是支持更好的枚举值访问范围。不多不少!

摆脱不能为枚举(类)声明方法的限制的方法是,使用std::bitset(包装类)或位域联合。

联合,这样的位域联合可以有方法(请看这里的限制!)。

我有一个示例,如何将位掩码值(如上所示)转换为相应的位索引,可以沿着std::bitset在这里使用:BitIndexConverter.hpp 我发现这对于提高一些基于“旗帜”决策的算法的可读性非常有用。

把注意力集中在问题的描述上,而不是标题,一个可能的答案是

struct LowLevelMouseEvent {
    enum Enum {
        mouse_event_uninitialized = -2000000000, // generate crash if try to use it uninitialized.
        mouse_event_unknown = 0,
        mouse_event_unimplemented,
        mouse_event_unnecessary,
        mouse_event_move,
        mouse_event_left_down,
        mouse_event_left_up,
        mouse_event_right_down,
        mouse_event_right_up,
        mouse_event_middle_down,
        mouse_event_middle_up,
        mouse_event_wheel
    };
    static const char* ToStr (const type::LowLevelMouseEvent::Enum& event)
    {
        switch (event) {
            case mouse_event_unknown:         return "unknown";
            case mouse_event_unimplemented:   return "unimplemented";
            case mouse_event_unnecessary:     return "unnecessary";
            case mouse_event_move:            return "move";
            case mouse_event_left_down:       return "left down";
            case mouse_event_left_up:         return "left up";
            case mouse_event_right_down:      return "right down";
            case mouse_event_right_up:        return "right up";
            case mouse_event_middle_down:     return "middle down";
            case mouse_event_middle_up:       return "middle up";
            case mouse_event_wheel:           return "wheel";
            default:
                Assert (false);
                break;
        }
        return "";
    }
};

根据jtlim的回答

想法(解决方案)

enum ErrorType: int {
  noConnection,
  noMemory
};

class Error {
public:
  Error() = default;
  constexpr Error(ErrorType type) : type(type) { }

  operator ErrorType() const { return type; }
  constexpr bool operator == (Error error) const { return type == error.type; }
  constexpr bool operator != (Error error) const { return type != error.type; }    
  constexpr bool operator == (ErrorType errorType) const { return type == errorType; }
  constexpr bool operator != (ErrorType errorType) const { return type != errorType; }

  String description() { 
    switch (type) {
    case noConnection: return "no connection";
    case noMemory: return "no memory";
    default: return "undefined error";
    }
 }

private:
  ErrorType type;
};

使用

Error err = Error(noConnection);
err = noMemory;
print("1 " + err.description());

switch (err) {
  case noConnection: 
    print("2 bad connection");
    break;
  case noMemory:
    print("2 disk is full");
    break;
  default: 
    print("2 oops");
    break;
}

if (err == noMemory) { print("3 Errors match"); }
if (err != noConnection) { print("4 Errors don't match"); }

是的,它们可以,但是你需要创建一个包装器类,例如:

#include <iostream>
using namespace std;

class Selection {
    public: 
        enum SelectionEnum {
            yes,
            maybe,
            iDontKnow,
            canYouRepeatTheQuestion
        };
        Selection(SelectionEnum selection){value=selection;};
        string toString() {
            string selectionToString[4]={
                "Yes",
                "Maybe",
                "I don't know",
                "Can you repeat the question?"
            };
            return selectionToString[value];
        };
    private:
        SelectionEnum value;
};


int main(){
    Selection s=Selection(Selection::yes);
    cout<<s.toString()<<endl;
    return 0;
}