Is there a way in C++ to extend/”inherit” enums?
I.E:
enum Enum {A,B,C};
enum EnumEx : public Enum {D,E,F};
or at least define a conversion between them?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, there is not.
enumare really the poor thing in C++, and that’s unfortunate of course.Even the
class enumintroduced in C++0x does not address this extensibility issue (though they do some things for type safety at least).The only advantage of
enumis that they do not exist: they offer some type safety while not imposing any runtime overhead as they are substituted by the compiler directly.If you want such a beast, you’ll have to work yourself:
MyEnum, that contains an int (basically)you may now extend your class (adding named constructors) at will…
That’s a workaround though, I have never found a satistifying way of dealing with an enumeration…