Extending an enum?

That would be known as a "dynamic enum". To the best of my knowledge, nothing like this exists in C++. However, since we're using C++ and not C, you could do something like this:

#include <string>
#include <map>

std::map<std::string, int> myMap;
myMap["DRESSING_OLIVES"] = 0;
myMap["DRESSING_CHEESE"] = 1;
myMap["PEPPER_TOPPING"] = 2;

This is closest to what you want: Base enum class inheritance


Since enums are typically handled as some size of int in the compiler, all you have to do is later make

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1,
    Pepperoni = 2
};

or you could allow it to count

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1,
    Pepperoni
};

You could, if neither of those is acceptable for some reason, use math (Cheese + 1). You can play around with the enum in almost any way you could with a numeric value.

Note that the enumerator you use is typically baked into the code by the compiler, it doesn't show up as its name, simply value. Thus, modifying (extending) the enumerator later on will not effect code that has been built.

I think it's legal syntax to use an enumeration in another enumerator, with casts, but I've never tried it. This may work, but is kind of ugly:

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1
};

enum OtherPizzaDressings
{
    Start = (OtherPizzaDressings)PizzaDressing::Cheese;
    Pepperoni
};

Tags:

C++

Enums