473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

enumerations: subenumerations?

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
Jul 22 '05 #1
3 1254
* nz******@cs.mun.ca (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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
"Neil Zanella" <nz******@cs.mun.ca> wrote
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
Jul 22 '05 #3

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
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.

Thanks,

neil

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
3
by: Neil Zanella | last post by:
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...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.