Extending enums in C++?

前端 未结 10 1073
挽巷
挽巷 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:20

    I do this '

    '''
        enum OPC_t // frame Operation Codes
        {
          OPC_CVSND = 0 // Send CV value
        , OPC_CVREQ = 1 // Request CV (only valid for master app)
        , OPC_COMND = 2 // Command
        , OPC_HRTBT = 3 // Heart Beat
        };
        enum rxStatus_t     // this extends OPC_t
        {
          RX_CVSND = OPC_CVSND  // Send CV value
        , RX_CVREQ = OPC_CVREQ  // Request CV
        , RX_COMND = OPC_COMND  // Command
        , RX_HRTBT = OPC_HRTBT  // Heart Beat
        , RX_NONE       // No new Rx
        , RX_NEWCHIP        // new chip detected
        };
    ''''
    
    0 讨论(0)
  • 2020-11-29 05:21

    I've solved in this way:

    typedef enum
    {
        #include "NetProtocols.def"
    } eNetProtocols, eNP;
    

    Of course, if you add a new net protocol in the NetProtocols.def file, you have to recompile, but at least it's expandable.

    0 讨论(0)
  • 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.)

    0 讨论(0)
  • 2020-11-29 05:23

    Actually you can extend enums in a round about way.

    The C++ standard defines the valid enum values to be all the valid values of the underlying type so the following is valid C++ (11+). Its not Undefined Behaviour, but it is very nasty - you have been warned.

    #include <cstdint>
    
        enum Test1:unit8_t {
            Value1 =0,
            Value2 =1
        };
    
        constexpr auto Value3 = static_cast<Test1>(3);
        constexpr auto Value4 = static_cast<Test1>(4);
        constexpr auto Value5 = static_cast<Test1>(5);
    
    
    Test1 fn(Test1 val){
      switch(val){
        case Value1:
        case Value2:
        case Value3:
        case Value4:
           return Value1;
        case Value5:
           return Value5; 
      } 
    }
    
    int main(){
      return static_cast<uint8_t>(fn(Value5));
    } 
    

    Note that most of the compilers don't consider the additional values as part of the set for generating warnings about missing enums values in switch statements.So clang and gcc will warn if Value2 is missing but will do nothing if Value4 is missing in the above switch statement.

    0 讨论(0)
  • 2020-11-29 05:26

    Just an idea:

    You could try to create an empty class for each constant (maybe put them all in the same file to reduce clutter), create one instance of each class and use the pointers to these instances as the "constants". That way, the compiler will understand inheritance and will perform any ChildPointer-to-ParentPointer conversion necessary when using function calls, AND you still get type-safety checks by the compiler to ensure no one passes an invalid int value to functions (which would have to be used if you use the LAST value method to "extend" the enum).

    Haven't fully thought this through though so any comments on this approach are welcome.

    And I'll try to post an example of what I mean as soon as I have some time.

    0 讨论(0)
  • 2020-11-29 05:33

    http://www.codeproject.com/KB/cpp/InheritEnum.aspx goes over a method to created an expanded enum.

    0 讨论(0)
提交回复
热议问题