13

Say I create an enum but eventually someone wants to add items to that enum, what does one do? ex:

// blah.hpp

enum PizzaDressing {
    DRESSING_OLIVES = 0,
    DRESSING_CHEESE = 1
};

and in my FunkyPizza class, there might be pepper toppings.

So how could I add peppers without obviously modifying the original enum?

Thanks.

Seva
  • 1,631
  • 2
  • 18
  • 23
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • possible duplicate of [base enum class inheritance](http://stackoverflow.com/questions/644629/base-enum-class-inheritance) – DuckMaestro Mar 20 '13 at 22:28

4 Answers4

8

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

Community
  • 1
  • 1
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Sorry thread necromancy, but I must emphasize that this is a much better method than the accepted answer describes. It allows to extend the enum in a way which makes it impossible to pass extended values to old functions which cannot handle them. – antipattern Jul 22 '16 at 14:44
  • 1
    @antipattern: there is no thread necromancy on SO, because there's no threads, only questions, answers and comments ;). – rubenvb Jul 22 '16 at 14:47
5

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;
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
  • 7
    It must be noted that the overhead of using a map is much bigger than using a plain `enum`. `map` is a data structure built and maintained dynamically during execution: each access on a `map` element is a function call. `enum` is a new compile time type, that is as complex as an `int`. – lvella Apr 16 '11 at 02:16
  • Yeah, this map of strings will be extremely slow compared to a normal enum. And there's no "dynamic enum" in C# (I should know; I wrote the workaround for that problem: http://www.codeproject.com/KB/cs/symbol.aspx) – Qwertie Apr 16 '11 at 06:06
  • My bad, I googled "dynamic enum" and a bunch of C# results came up so I assumed. I did a little more research and I guess all of those results were people asking this same question. edited. Thanks – Andrew Rasmussen Apr 16 '11 at 07:56
  • Isn't `std::map` `constexpr` since C++11 ? If so, would it be fine to use it as a dynamic-enum implementation ? – Telokis Apr 16 '16 at 08:40
5

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
};
ssube
  • 47,010
  • 7
  • 103
  • 140
1

You can't dynamically modify an enum, because it only defines a new type resolved at compile time. They are mnemonics for the programmer, at compilation they are translated to numbers.

That said, you can use any number not used by the original enum to represent whatever you want:

PizzaDressing a;
a = (PizzaDressing)5;
lvella
  • 12,754
  • 11
  • 54
  • 106
  • But then you can simply use numbers everywhere. So what's the point? It'd be better to use compiler/language features for keeping track of it, instead of doing it by nasty-casty hand. – SasQ Aug 12 '12 at 04:11
  • I don't know. I have no idea of what you are trying to do. I am just explaining the compiler feature so one may decide how to proceed and use it. Maybe one have a `map` where some names have values defined dynamically above 100, and bellow 100 there is a statically defined `enum`. Anyway, this is just one situation from an infinite number of possibilities a programmer may make up and use the language on its own favor. – lvella Aug 13 '12 at 15:50