TITLE: extending enums (Newsgroups: comp.lang.c++.moderated) PROBLEM: Ken Brady A class library which I am using defines an enumerated type, e.g. /* fruits.h */ enum Fruits {orange, apple, banana}; // Followed by classes corresponding to Fruits. I am creating some descendents of this libary's classes, and need more choices. I want to extend this enumeration, something like: enum Fruits {guava = banana+1, kiwi, pomegranate}; Of course, this doesn't work. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 30 Jun 96 Under new C++ rules, an enum object can take on any numerical value that falls in the extended range of the enum. The extended range consists of the values which can be represented in the number of bits needed to represent the defined values of the enum. You can't extend an enum, but you can define new constants of the enum type. Example: enum Fruits {orange, apple, banana, maxFruits=10000}; It takes 15 bits to represent all the Fruits, so a Fruits object or constant can take on any value in the range 0-32767. You can then define const Fruits guava = Fruits(banana+1); const Fruits kiwi = Fruits(guava+1); and so on. Another use for this facility: enum Errorkind { warning=0, severe=1500, fatal=3000 }; Now you can create constants of type Errorkind in the range 1-1499 for warnings, 1501-2999 for severe errors, and 3001-4095 for fatal errors. The first question you should ask, however, is why the design calls for extending an enum type. If you are using switch statments based on the enum type, the design very likely is not properly factored. You should probably use virtual functions to accomplish whatever the switches are being used for. In the second example we are not extending an enum based on derived classes. We are extending it as the program evolves and we want to identify new errors. Those additions are likely to be independent of any class hierarchy. In either case, const ints would serve, but a reason to choose an enum type is added type safety. You can't accidently pass a numerical value to a function parameter of type Errorkind.