1

Is it possible to extend enum declared in a parent class?

Here are two classes:

    class A {
        enum States {
            init,
            done
        }
    }

    class B : public A {
        enum States {
            // ???
            archived
        }
    }

I’d like to have a few extra states in class B. Is it possible? What’s the proper way?

zhekaus
  • 3,126
  • 6
  • 23
  • 46
  • Maybe `archived = A::States::done + 1`? – vahancho Feb 28 '20 at 15:04
  • 3
    Does this answer your question? [Extending enums in C++?](https://stackoverflow.com/questions/1804840/extending-enums-in-c) – Sugar Feb 28 '20 at 15:05
  • Related: [https://stackoverflow.com/questions/644629/base-enum-class-inheritance](https://stackoverflow.com/questions/644629/base-enum-class-inheritance) – drescherjm Feb 28 '20 at 15:07
  • [std::variant](https://en.cppreference.com/w/cpp/utility/variant) can be used to hold 2 or more different enum types. – Eljay Feb 28 '20 at 16:26
  • It is possible to declare constants of the same type as your enum, and extend it that way, but they don't have the same namespace relationship as, for example, c+=11 `enum class`. You need to also be aware that the compiler's storage of your enum depends on the range it's member values, so you should either specify the underlying type using c++11 syntax or declare an _MAX member when first declaring the enum. – Gem Taylor Mar 02 '20 at 14:19

0 Answers0