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 enum class instead of enums).
http://www.cplusplus.com/doc/tutorial/other_data_types/ doesn't mention anything about methods However, I was under the impression that any type of class can have methods.
The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.
Below code uses enums with defined methods: We should define methods as abstract methods and then we have to implement defferent flavours/logic based on each enum members. Because of declaring abstract method at the enum level; all of the enum members require to implement the method.
An enum just spills its contents into the enclosing scope, and is basically a const static integer. This means that the first element of any default enum is the same using the == operator. Enum classes have their own scope, and don't pollute the namespace that they are in.
In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used.
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
.
While the answer that "you can't" is technically correct, I believe you may be able to achieve the behavior you're looking for using the following idea:
I imagine that you want to write something like:
Fruit f = Fruit::Strawberry; f.IsYellow();
And you were hoping that the code looks something like this:
enum class Fruit : uint8_t { Apple, Pear, Banana, Strawberry, bool IsYellow() { return this == Banana; } };
But of course, it doesn't work, because enums can't have methods (and 'this' doesn't mean anything in the above context)
However, if you use the idea of a normal class containing a non-class enum and a single member variable that contains a value of that type, you can get extremely close to the syntax/behavior/type safety that you want. i.e.:
class Fruit { public: enum Value : uint8_t { Apple, Pear, Banana, Strawberry }; Fruit() = default; constexpr Fruit(Value aFruit) : value(aFruit) { } #if Enable switch(fruit) use case: // Allow switch and comparisons. constexpr operator Value() const { return value; } // Prevent usage: if(fruit) explicit operator bool() = delete; #else constexpr bool operator==(Fruit a) const { return value == a.value; } constexpr bool operator!=(Fruit a) const { return value != a.value; } #endif constexpr bool IsYellow() const { return value == Banana; } private: Value value; };
Now you can write:
Fruit f = Fruit::Strawberry; f.IsYellow();
And the compiler will prevent things like:
Fruit f = 1; // Compile time error.
You could easily add methods such that:
Fruit f("Apple");
and
f.ToString();
can be supported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With