Home > Enterprise >  An issue on switch/case statements with a class implementing enums (to be derived)
An issue on switch/case statements with a class implementing enums (to be derived)

Time:06-24

after some searching on the Net, I found a satisfying, at least for me, solution for C 14 enums inheritance, as you can see in the code below, where only base class is mentioned. Everything works fine except for switch/case usage where a compilation error occurs.

May someone suggest a simple modification to MyEnum class in order to be used in switch/case statements, too?

Many thanks.

#include <iostream>
#include <cstdint>

class MyEnum
{
    public:

        static const MyEnum VAL_1;
        static const MyEnum VAL_2;
        static const MyEnum VAL_3;

        MyEnum() {}

        explicit MyEnum(uint8_t val) : value(val) {}

        virtual ~MyEnum() {}

        bool operator<(const MyEnum& other) const { return value < other.value; }
        bool operator==(const MyEnum& other) const { return value == other.value; }
        bool operator!=(const MyEnum& other) const { return !(operator==(other)); }

        operator uint8_t() const { return value; }

    protected:

        static const uint8_t Val1 = 1;
        static const uint8_t Val2 = 2;
        static const uint8_t Val3 = 3;

        uint8_t value;
};

const MyEnum MyEnum::VAL_1(Val1);
const MyEnum MyEnum::VAL_2(Val2);
const MyEnum MyEnum::VAL_3(Val3);

int main()
{
    MyEnum e;

    e = MyEnum::VAL_1;

    if (e == MyEnum::VAL_1)
        std::cout << "Compiles" << std::endl;

    switch (e)
    {
        case MyEnum::VAL_1:
        {
            std::cout << "Doesn't compile" << std::endl;
        }
    }

    exit(0);
}

CodePudding user response:

Slight modifications to compiles:

class MyEnum
{
    public:

        static const MyEnum VAL_1;
        static const MyEnum VAL_2;
        static const MyEnum VAL_3;

        MyEnum() {}

        constexpr explicit MyEnum(uint8_t val) : value(val) {}

        bool operator<(const MyEnum& other) const { return value < other.value; }
        bool operator==(const MyEnum& other) const { return value == other.value; }
        bool operator!=(const MyEnum& other) const { return !(operator==(other)); }

        constexpr operator uint8_t() const { return value; }

    protected:

        static const uint8_t Val1 = 1;
        static const uint8_t Val2 = 2;
        static const uint8_t Val3 = 3;

        uint8_t value;
};

constexpr MyEnum MyEnum::VAL_1(Val1);
constexpr MyEnum MyEnum::VAL_2(Val2);
constexpr MyEnum MyEnum::VAL_3(Val3);

Demo

CodePudding user response:

What about using Pete Becker' solution ? Add an enumerated type -and associated conversion operators- ? You can do it this way

#include <iostream>
#include <cstdint>

class MyEnum
{
public:
  typedef enum {
    VAL_1 = 1,
    VAL_2,
    VAL_3
  } vale;
  MyEnum() {}
  explicit MyEnum(uint8_t val) : value((vale)val) {}
  virtual ~MyEnum() {}
  bool operator<(const MyEnum& other) const { return value < other.value; }
  bool operator==(const MyEnum& other) const { return value == other.value; }
  bool operator!=(const MyEnum& other) const { return !(operator==(other)); }
 //replaced by operator vale :
 //operator uint8_t() const { return value; }
 MyEnum& operator = (const vale& v) {
   value = v;
   return *this;
 }
 operator vale () const {
   return value;
 }
 //op ==, !=... to be written
protected:
  vale value;
};

int main() {
  MyEnum e;
  e = MyEnum::VAL_1;
  if (e == MyEnum::VAL_1)
    std::cout << "Compiles" << std::endl;
  switch (e) {
  case MyEnum::VAL_1:
     std::cout << "compiles now" << std::endl;
     break;
  }
  exit(0);
}
  • Related