Created
August 2, 2015 20:20
-
-
Save iscgar/c18e308b98bee9752754 to your computer and use it in GitHub Desktop.
"Inheritable" enums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
struct BaseEnum | |
{ | |
public: | |
operator int() const { return this->type_; } | |
protected: | |
inline BaseEnum(int type) : type_(type) {} | |
private: | |
int type_; | |
}; | |
struct DefaultEnumValue : public BaseEnum | |
{ | |
public: | |
inline DefaultEnumValue() : BaseEnum(0) {} | |
}; | |
struct ExtendedEnum : public BaseEnum | |
{ | |
enum EExtendedEnum | |
{ | |
FirstValue, | |
SecondValue | |
}; | |
explicit inline ExtendedEnum(EExtendedEnum type) : BaseEnum(static_cast<int>(type)) {} | |
}; | |
int main() | |
{ | |
std::cout << "DefaultEnumValue value: " << DefaultEnumValue() << " (" << sizeof(DefaultEnumValue) << " bytes)" << std::endl; | |
std::cout << "ExtendedEnum::SecondValue value: " << ExtendedEnum(ExtendedEnum::SecondValue) << " (" << sizeof(ExtendedEnum) << " bytes)" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The basic idea can be extended into an even more horrifying code, such as:
But that's just evil.