Topic: Inheriting Enums
Author: wslade@atlanta.com
Date: 1995/10/18 Raw View
I have been using enums to define error numbers within a specific class.
I now want to derive a class from this base. I would also like to derive
the enum from the base.
For example
class A
{
enum AError
{
eErrorOpen, eErrorClose
}
};
class B : public A
{
enum BError : AError
{
eErrorPipeOpen // Would start at one more than AError::eErrorClose
}
};
Has anyone ever wanted to do this or is there another way of doing
the same thing.
Is this a possibility of being added to the C++ standard.
----------------------------------------------------------------------
Warwick Slade | wslade@atlanta.com
Software Consultant | (wk) +1-770-3938646
Computer Consulting Services Corporation |
----------------------------------------------------------------------
Plus Tax, Plus Tax, Plus Tax, Plus Tax, Plus Tax, Plus Tax, Plus Tax.
----------------------------------------------------------------------
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Author: Christopher Sweeney <csweeney@tcinc.com>
Date: 1995/10/18 Raw View
wslade@atlanta.com wrote:
>I have been using enums to define error numbers within a specific class.
>I now want to derive a class from this base. I would also like to derive
>the enum from the base.
>
>For example
>
>class A
>{
> enum AError
> {
> eErrorOpen, eErrorClose
> }
>};
>
>class B : public A
>{
> enum BError : AError
> {
> eErrorPipeOpen // Would start at one more than AError::eErrorClose
> }
>};
>
>Has anyone ever wanted to do this or is there another way of doing
>the same thing.
It is fairly easy to manage the enums by convention, e.g.
enum AError
{
eErrorOpen, eErrorClose, AErrorLast
}
enum BError
{
BErrorFirst = AErrorLast, eErrorPipeOpen // Would start at one more than
AError::eErrorClose
}
However, you cannot declare a member function that returns an AError OR a
BError (which is
probably what you are trying to do). The closest thing I've done invloves
introducing a baseclass
and then deriving from it. This may be quite a bit of overhead compared to
returning an int, but
it does have the advantage of a reasonably easy transition to exception
handling.
C. Sweeney
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/10/19 Raw View
In article MAA13614@atlanta.com, wslade@atlanta.com writes:
>I have been using enums to define error numbers within a specific class.
>I now want to derive a class from this base. I would also like to derive
>the enum from the base.
>
> ...
>
>Has anyone ever wanted to do this or is there another way of doing
>the same thing.
>Is this a possibility of being added to the C++ standard.
Things like this have been discussed informally, such as being able to
derive from non-class types in general. For example, it would be nice
to be able to write
class MyInteger : public int { ... };
Such capability will not be added to the language in this round. Maybe
C++ 2005 :-)
In the mean time, you can get the the effect you want because additional
constants of the enum type may be defined as long as they fall within the
range of the original enum (extended to a power of 2 less one).
For example:
class chili {
public:
enum hotness { low=0, medium=500, high=1000, scary=10000 };
};
Now you can define constants of type chile::hotness with any value
in the range 0 through 32767, the extended range of hotness.
class jalapeno : public chili {
public:
static const hotness hot_jal = medium+300;
static const hotness med_jal = medium;
static const hotness mild_jal = medium-200;
};
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]