(ref.doc)clamage 270395

Next smeyers 300395 Prev: rmartin 140395 Up: Usenet

From: [email protected] (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: "Subclassing" Enums
Date: 27 Mar 1995 20:04:39 GMT

C++ now has the rule that any value in the extended range of the enum
may be applied to anything of that enum type. The "extended range"
is the range implied by the number of bits needed to represent all the
declared values. To ensure a maximum extended range, you can declare
a dummy value. (To be able to subclass an enum, you would have to do
that anyway in the original enum.)

Example:

  enum error_code { informational=0, warning=1000, error=2000, fatal=3000 };

Now you can add your own codes as constants:
	const error_code weekday = 100;            // informational
	const error_code might_overflow = 1100;    // warning
	const error_code conflicting_range = 2100; // error
	const error_code cyanide = 3100;           // fatal

These can be extended at will, in this case up to 4095.

The new rule about const static members makes these work even more like
enums in classes, including having class scope:

	class base {
		enum error_code { informational=0, warning=1000,
				  error=2000, fatal=3000 };
	};

	class derived : public base {
		static const error_code weekday = 100;
		...
	};

Now you can refer to base::error or derived::weekday with the same syntax
and semantics.

automatically generated by info2www version 1.2.2.8