TITLE: extending enum values (Newsgroups: comp.std.c++, 1 Feb 97) TRIBBLE: David R Tribble >I wrote [concerning enums in derived classes that can inherit from enums in >the base class]: >|> ... Using colors for enum values is a bit contrived; a more realistic >|> example would be error codes, where Base defined some basic error codes >|> and Derived added to the list. If Derived's enum could inherit from >|> Base's enum, then Derived's enum values could be guaranteed unique, even >|> if Base's enum values were changed. KANZE: James Kanze >> Except that you don't want to do this, generally, since it means that a >> Derived can no longer be substituted for a Base (since it may return >> error codes the user is not prepared to handle, being unknown to him). TRIBBLE: >That implies that a base class must define all the error codes that it and >any of its descendants will ever use. CLAMAGE: Stephen.Clamage@eng.sun.com (Steve Clamage) Not at all. C++ requires two things of enums that C does not: 1. The implementation of an enum type must use enough bits to store all values in the full range of the defined enumerators (the representation must be binary, as with integers). 2. All values representable in that number of bits are allowed for the enum type. Thus, we can later define constants of the enum type that do not match any of the enumerators, and use them as if they were predefined enumerators. Example: enum diagnostic { warning=0, serious=3000, fatal=6000 }; We can later, in any suitable scope (e.g. a derived class), add new named constants in the range 0-8191: const diagnostic not_elegant = diagnostic(warning+1); const diagnostic rule_violation = diagnostic(serious+1); const diagnostic capacity_exceeded = diagnostic(fatal+1); You can use these new constants in exactly the ways you would use "serious", for example.