Normally you can define a cast for a class by using the following syntax:
class Test { public: explicit operator bool() { return false; } };
Is there a way to do this or something similar for an enum class
?
Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.
Not possible. There is no inheritance with enums. You can instead use classes with named const ints.
The stringify() macro method is used to convert an enum into a string. Variable dereferencing and macro replacements are not necessary with this method. The important thing is that, only the text included in parenthesis may be converted using the stringify() method.
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
No, it's not.
Actually, an enum class
is no class at all. The class
keyword is only used because suddenly changing the unscoped enum
to a scoped enum
would have mean reworking all enums codes. So the committee decided that to distinguish between new-style and old-style enums, the new ones would be tagged with class
, because it's a keyword already so no enum
could have been named class
in C++. They could have picked another, it would not have made much more sense anyway.
However, despite the class
keyword they are still regular enums in that only enumerators (and potentially values assigned to them) are allowed within the brackets.
No, but you can make a normal class type act like an enum class, using constexpr
members and constructors. And then you can add all the additional member functions you want.
Proof that it can work even with switch
:
#include <iostream> struct FakeEnum { int x; constexpr FakeEnum(int y = 0) : x(y) {} constexpr operator int() const { return x; } static const FakeEnum A, B, Z; }; constexpr const FakeEnum FakeEnum::A{1}, FakeEnum::B{2}, FakeEnum::Z{26}; std::istream& operator>>(std::istream& st, FakeEnum& fe) { int val; st >> val; fe = FakeEnum{val}; return st; } int main() { std::cout << "Hello, world!\n"; FakeEnum fe; std::cin >> fe; switch (fe) { case FakeEnum::A: std::cout << "A\n"; break; case FakeEnum::B: std::cout << "B\n"; break; case FakeEnum::Z: std::cout << "Z\n"; break; } }
Proof that working with switch
does not require implicit interconversion with int
:
#include <iostream> /* pseudo-enum compatible with switch and not implicitly convertible to integral type */ struct FakeEnum { enum class Values { A = 1, B = 2, Z = 26 }; Values x; explicit constexpr FakeEnum(int y = 0) : FakeEnum{static_cast<Values>(y)} {} constexpr FakeEnum(Values y) : x(y) {} constexpr operator Values() const { return x; } explicit constexpr operator bool() const { return x == Values::Z; } static const FakeEnum A, B, Z; }; constexpr const FakeEnum FakeEnum::A{Values::A}, FakeEnum::B{Values::B}, FakeEnum::Z{Values::Z}; std::istream& operator>>(std::istream& st, FakeEnum& fe) { int val; st >> val; fe = FakeEnum(val); return st; } int main() { std::cout << "Hello, world!\n"; FakeEnum fe; std::cin >> fe; switch (fe) { case FakeEnum::A: std::cout << "A\n"; break; case FakeEnum::B: std::cout << "B\n"; break; case FakeEnum::Z: std::cout << "Z\n"; break; } // THIS ERRORS: int z = fe; }
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