enumerations: subenumerations?

N

Neil Zanella

Hello,

C++ supports the concept of inheritance quite well when it comes to classes.
However, what about other things like enumerations? Suppose I have two functions
foo and bar. I want to restrict input to bar to only a subset of the enueration
types declared in X. But I cannot subclass X from Y!!!

enum X {A, B, C, D};

enum Y {B, C};

void foo(X x);

void bar(Y y);

So what would you recommend. One obvious thing is to have bar return a boolean
and return false if the specified y is an element of (X setminus Y). Not as
neat as I would have liked.

Any recommendations??

Thanks,

neil
 
A

Alf P. Steinbach

* (e-mail address removed) (Neil Zanella) schriebt:
Hello,

C++ supports the concept of inheritance quite well when it comes to classes.
However, what about other things like enumerations? Suppose I have two functions
foo and bar. I want to restrict input to bar to only a subset of the enueration
types declared in X. But I cannot subclass X from Y!!!

enum X {A, B, C, D};

enum Y {B, C};

void foo(X x);

void bar(Y y);

So what would you recommend.

If you actually need that kind of thing (which is highly doubtful) then
you need a simple Range class.
 
C

Claudio Puviani

Neil Zanella said:
Hello,

C++ supports the concept of inheritance quite well when
it comes to classes.

First off, inheritance is meant to extend an interface, not to restrict it.
Restricting an interface would violate the Liskov Substitution Principle.
However, what about other things like enumerations? Suppose
I have two functions foo and bar. I want to restrict input to bar
to only a subset of the enueration types declared in X. But I cannot
subclass X from Y!!!

enum X {A, B, C, D};

enum Y {B, C};

void foo(X x);

void bar(Y y);

So what would you recommend. One obvious thing is to have bar
return a boolean and return false if the specified y is an element of
(X setminus Y). Not as neat as I would have liked.

Sadly, the mechanism for this -- unless you programmatically generate
enums -- is a tedious one:

Firstly, you need to wrap your enums in a class or a namespace, like this:

struct X {
enum Value { A, B, C, D };
};

Secondly, you need to initialize the derivative enum with values from the
first:

struct Y {
enum Value { B = X::B, C = X::B };
};

Your functions now look like:

void foo(X::Value x);
void bar(Y::Value y);

Claudio Puviani
 
N

Nick Hounsome

Neil Zanella said:
Hello,

C++ supports the concept of inheritance quite well when it comes to classes.
However, what about other things like enumerations? Suppose I have two functions
foo and bar. I want to restrict input to bar to only a subset of the enueration
types declared in X. But I cannot subclass X from Y!!!

enum X {A, B, C, D};

enum Y {B, C};

void foo(X x);

void bar(Y y);

So what would you recommend. One obvious thing is to have bar return a boolean
and return false if the specified y is an element of (X setminus Y). Not as
neat as I would have liked.

Any recommendations??

This is usually a sign of poor design.
What are the real enums and functions?

There is no easy way other than stuff like
enum All { ALL_A,ALL_B,ALL_C,ALL_D };
enum X { A=ALL_A, B = ALL_B .......etc
enum Y { B=ALL_B, C=ALL_C };
Note that if the enums are in a class you can make All protected or private
and it will still work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top