Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending an enum?

Tags:

c++

enums

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.

like image 501
jmasterx Avatar asked Apr 16 '11 02:04

jmasterx


People also ask

Can you extend an enum?

Enums can be extended in order to add more values to the enumeration list in which case the Extensible property must be set to true .

Can we extend an enum to add elements?

No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.

Can a enum extend another class?

All enumerations internally extend a class named Enum the base class of all the language enumeration types. Since Java doesn't support multiple inheritance you cannot extend another class with enumeration if you try to do so, a compile time error will be generated.

Can you extend enums C#?

Since it's not a class, it cannot be extended and its defined values are limited to a small set of primitive types ( byte , sbyte , short , ushort , int , uint , long , ulong ). For instance, sometimes it is very convenient to get the list of enum values and display it in a combobox.


1 Answers

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;
like image 138
lvella Avatar answered Oct 05 '22 03:10

lvella