Can a C++ enum class have methods?

前端 未结 7 784
抹茶落季
抹茶落季 2020-12-12 17:44

I have an enum class with two values, and I want to create a method which receives a value and returns the other one. I also want to maintain type safety(that\'s why I use e

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 18:21

    No, they can't.

    I can understand that the enum class part for strongly typed enums in C++11 might seem to imply that your enum has class traits too, but it's not the case. My educated guess is that the choice of the keywords was inspired by the pattern we used before C++11 to get scoped enums:

    class Foo {
    public:
      enum {BAR, BAZ};
    };
    

    However, that's just syntax. Again, enum class is not a class.

提交回复
热议问题