Thread View: comp.std.c++
5 messages
5 total messages
Started by wslade
Wed, 18 Oct 1995 16:58
Inheriting Enums
Author: wslade
Date: Wed, 18 Oct 1995 16:58
Date: Wed, 18 Oct 1995 16:58
36 lines
1739 bytes
1739 bytes
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. ]
Re: Inheriting Enums
Author: Christopher Sweeney
Date: Wed, 18 Oct 1995 19:58
Date: Wed, 18 Oct 1995 19:58
51 lines
2216 bytes
2216 bytes
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. ]
Re: Inheriting Enums
Author: Steve Clamage
Date: Thu, 19 Oct 1995 04:02
Date: Thu, 19 Oct 1995 04:02
44 lines
2156 bytes
2156 bytes
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. ]
Range types? (was Re: Inheriting Enums)
Author: Dr. Richard Botting
Date: Fri, 27 Oct 1995 22:30
Date: Fri, 27 Oct 1995 22:30
44 lines
1750 bytes
1750 bytes
Steve Clamage (clamage@Eng.Sun.COM) wrote on the old topic):
: For example:
: class chili {
: public:
: enum hotness { low=0, medium=500, high=1000, scary=10000 };
: };
So, a declaration like
enum range{first=1, last=20};
permits
for( range i=first; i<=last; i++)
whatever;
An hence... (forgive my ignorance below):
template <int first, int last> class range{
enum{ FIRST=first, LAST=last }v;
public:
range(){v=FIRST;}
range val(){return v}
void reset(){v=FIRST;}
bool ok(){return FIRST<=v && v<=LAST;}
range& operator++{ v++; return *this;}
...
}
for(range<0,17> i; ok(i); i++)
for(range<0,32>chn; ok(chn); chn++)
{ char c=char(chn);
whatever...
}
--
dick@csci.csusb.edu=rbotting@wiley.csusb.edu.
Find out what's new at http://www.csci.csusb.edu/doc/www.sites.html
Disclaimer:`CSUSB may or may not agree with this message`.
Copyright(1995):Copy this freely but include the following link to the
<a href="http://www.csci.csusb.edu/dick/signature.html">author's signature</a>
---
[ 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. ]
Re: Range types? (was Re: Inheriting Enums)
Author: Jak Kirman
Date: Sun, 29 Oct 1995 01:33
Date: Sun, 29 Oct 1995 01:33
136 lines
5275 bytes
5275 bytes
>>>>> "Richard" == Richard Botting <dick@silicon.csci.csusb.edu> writes:
In article <46o780$2au@news.csus.edu> dick@silicon.csci.csusb.edu (Dr. Richard Botting) writes:
Richard> template <int first, int last> class range{
Richard> enum{ FIRST=first, LAST=last }v;
Richard> public:
Richard> range(){v=FIRST;}
Richard> range val(){return v}
Richard> void reset(){v=FIRST;}
Richard> bool ok(){return FIRST<=v && v<=LAST;}
Richard> range& operator++{ v++; return *this;}
Richard> ...
Richard> }
Richard> for(range<0,17> i; ok(i); i++)
Richard> for(range<0,32>chn; ok(chn); chn++)
Richard> { char c=char(chn);
Richard> whatever...
Richard> }
It seems to me that a range is a container, and that you really want to
iterate through the container. Something like:
// 0 to 16, for consistency with STL ranges, which are [start,end)
Range<int> r (0, 17);
cout << "copying range to stdout with a for loop" << endl;
for(Range<int>::iterator i = r.begin(); i != r.end(); i++)
cout << *i << endl;
cout << "copying range to stdout with STL copy" << endl;
copy (r.begin(), r.end(), ostream_iterator<int> (cout, "\n"));
cout << "copying range to stdout backwards with STL copy" << endl;
copy (r.rbegin(), r.rend(), ostream_iterator<int> (cout, "\n"));
I don't see the advantage of making the bounds part of the template; I
think of Range<T> as being a type that represents any range of objects
of type T. This allows easy copying of ranges, which seems a useful
feature.
Here are the definitions for the code above. I have probably missed
some requirements, and maybe the iterators should be random_access; I am
not sure. Any comments?
// STL headers
#include <iterator.h>
#include <algo.h>
// Standard library headers
#include <iostream.h>
template <class T>
class Range
{
public:
class Iterator : public bidirectional_iterator<T,ptrdiff_t>
{
private:
T current;
public:
Iterator () { }
Iterator (const T& t) { current = t; }
Iterator operator++ () { ++current; return *this; }
Iterator operator++ (int) { Iterator tmp = *this; ++current; return tmp; }
Iterator operator-- (int) { --current; return *this; }
Iterator operator-- () { Iterator tmp = *this; --current; return tmp; }
T operator* () const { return current; }
friend bool operator== (const Iterator& i1, const Iterator& i2)
{ return *i1 == *i2; }
};
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef Iterator iterator;
typedef Iterator const_iterator;
typedef reverse_bidirectional_iterator<const_iterator, value_type,
reference, ptrdiff_t> reverse_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
private:
T first;
T last;
public:
Range() {} // empty range -- first and last will both be created with
// default constructor, so begin() and end() should test equal.
Range(const T& f, const T& l) {first = f; last = l;}
iterator begin() const { return iterator(first); }
iterator end() const { return iterator (last); }
reverse_iterator rbegin() const {return reverse_iterator(iterator(last-1)); }
reverse_iterator rend() const {return reverse_iterator (iterator(first-1)); }
};
main (int argc, char *argv[])
{
Range<int> r (0, 17); // 0 to 16, for consistency with STL [start,end)
cout << "copying range to stdout with a for loop" << endl;
for(Range<int>::iterator i = r.begin(); i != r.end(); i++)
cout << *i << endl;
cout << "copying range to stdout with STL copy" << endl;
copy (r.begin(), r.end(), ostream_iterator<int> (cout, "\n"));
cout << "copying range to stdout backwards with STL copy" << endl;
copy (r.rbegin(), r.rend(), ostream_iterator<int> (cout, "\n"));
// not terribly useful, but it works :-)
char **days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday", 0};
Range<char **> r (days[0], days[7]);
typedef Range<char **>::iterator Iterator;
for (Iterator i = r.begin(); i!= r.end(); i++)
cout << **i << endl;
}
Jak Kirman jak@cs.brown.edu http://www.cs.brown.edu/people/jak/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Every \item command in item_list must have an optional argument.
-- LaTeX p.168
---
[ 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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads