Extending enums in C++?

前端 未结 10 1101
挽巷
挽巷 2020-11-29 05:08

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

10条回答
  •  一生所求
    2020-11-29 05:22

    If you were able to create a subclass of an enum it'd have to work the other way around.

    The set of instances in a sub-class is a subset of the instances in the super-class. Think about the standard "Shape" example. The Shape class represents the set of all Shapes. The Circle class, its subclass, represents the subset of Shapes that are Circles.

    So to be consistent, a subclass of an enum would have to contain a subset of the elements in the enum it inherits from.

    (And no, C++ doesn't support this.)

提交回复
热议问题