Re: deriving a class - extending enum
werasm wrote:
On Dec 5, 10:39 pm, "Alf P. Steinbach" <al...@start.no> wrote:
Don't use enums.
Never?
...
Forget it. _DO_ use enums. The hierarchy of exception classes is a good
idea, but _not_ as a replacement for enum constants as exception codes.
An attempt to introduce a separate exception class for every concrete
exception type will only lead to a disaster. In the exception hierarchy
the classes themselves are supposed to serve for a rough division of
various exception types into relatively large groups or..., well...
"classes" of exceptions. The concrete exception type within the class is
still best described with a enum constant.
Your original idea of "extending" the enum in the derived class can be
easily implemented as follows
class BaseException {
public:
enum Type {
firstEnum,
secondEnum,
next_enum_,
};
int m_something;
...
};
class DerivedException : public BaseException {
typedef BaseException BASE;
public:
enum Type {
oneMoreEnum = BASE::next_enum_,
yetMoreEnum,
stillMoreEnum,
next_enum_
};
...
};
class DerivedDerivedException : public DerivedException {
typedef DerivedException BASE;
public:
enum Type {
oneAdditionalEnum = BASE::next_enum_,
anotherAdditionalEnum,
moreAdditionalEnum,
next_enum_
};
...
};
// And so on...
Note how the 'next_enum_' name is used in the above example and how
(together with the 'BASE' typedef) it makes all derived exception
classes to have uniform definitions.
Of course, in this case you are not really "extending" the base enum
type, but rather create another enum type with sequence of constants
that continue the inherited sequence. Of course, each enum is a separate
type, but that shouldn't worry you at all: simply disregard the enum
types entirely and use an 'int' value to describe the concrete exception
instead.
--
Best regards,
Andrey Tarasevich