Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Enumerations

Reply
Thread Tools

Enumerations

 
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-22-2004
Christopher Benson-Manica wrote:
> Peter van Merkerk <(E-Mail Removed)> spoke thus:
>
>>In case of a class you might consider putting the enum definition in the
>>private section of the class. That way it is clear that the enum is
>>intended for internal use only, and isn't accessible to the outside
>>world. Another advantage is that it is defined within the scope of the
>>class, so potential name clashes are avoided.

>
> Well, here's another question: If I put the enum definition in the
> class, can a subclass override that definition?


I'm afraid not.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      07-22-2004
Mark A. Gibbs posted:

> would work. but that is a case of directly and

maliciously
> circumventing the enumeration.



Month blah = Month(13);


> error::type nope = 3; // won't compile


= EnumName(3); //*will* compile, regardless if the enum
contains a 3.

-JKOp
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      07-22-2004
JKop <(E-Mail Removed)> spoke thus:

> = EnumName(3); //*will* compile, regardless if the enum
> contains a 3.


Granted, but why on earth would you want to do *that*?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      07-22-2004
Peter van Merkerk <(E-Mail Removed)> spoke thus:

> I'm afraid not.


No, no need to be afraid

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      07-22-2004
On Wed, 21 Jul 2004 19:46:21 +0000 (UTC), Christopher Benson-Manica
<(E-Mail Removed)> wrote:

>I'll try to explain what I want to do:
>
>I have foo.h and foo.cpp. Units that include foo.h will define an
>enumeration bar:
>
>enum bar { typeNone, typeBaz, typeQuux, ... , count };
>
>A method defined in foo.cpp needs to return typeNone. Is using
>
>static_cast< bar >( 0 )
>
>acceptable?
>
>Also, methods in foo.cpp need to return typeBaz, typeQuux, etc. Is
>there any way to specify that these are values of the bar enumeration
>without actually defining bar (since units that include foo.h will
>take care of that)?


All definitions of bar must be token for token identical according to
the one definition rule. If they aren't, you've got undefined
behaviour.

In your case you might just want to return some named constants, and
have other TUs cast them to whatever enum type they want (and
presumably a different one for each TU).

Tom
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-22-2004
Christopher Benson-Manica posted:

> JKop <(E-Mail Removed)> spoke thus:
>
>> = EnumName(3); //*will* compile, regardless if the enum

contains a
>> 3.

>
> Granted, but why on earth would you want to do *that*?
>


Because it can be done. If you write a function that takes
a month, then you've to check that it's >= 1 and <= 12.
Therefore, what use has an enum? Why not just use constant
variables?

namespace Month{
const unsigned char jan = 1,
feb = 2,
mar = 3,
apr = 4,
may = 5,
jun = 6,
//and so on


-JKop
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      07-22-2004
JKop <(E-Mail Removed)> wrote in news:zhPLc.5236$(E-Mail Removed):

> Christopher Benson-Manica posted:
>
>> JKop <(E-Mail Removed)> spoke thus:
>>
>>> = EnumName(3); //*will* compile, regardless if the enum

> contains a
>>> 3.

>>
>> Granted, but why on earth would you want to do *that*?
>>

>
> Because it can be done. If you write a function that takes


So can dereferencing a NULL pointer, and reinterpret_casting stuff between
incompatable types. Doesn't mean that it's defined behaviour.

> a month, then you've to check that it's >= 1 and <= 12.
> Therefore, what use has an enum? Why not just use constant
> variables?


No you don't. Your function simply takes a Month as a parameter. No need
to check for range. Your caller would have to invoke undefined behaviour
to break your function then.
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      07-22-2004

"Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
news:cdoc6j$ftb$(E-Mail Removed)...
> JKop <(E-Mail Removed)> spoke thus:
>
> > = EnumName(3); //*will* compile, regardless if the enum
> > contains a 3.

>
> Granted, but why on earth would you want to do *that*?


I think he means, how would you keep a user from inadvertently doing that
via a compiler error.

Jeff F


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      07-22-2004
Andre Kostur <(E-Mail Removed)> wrote:

[snip - debate over whether to use 'enum' for month names]

> No you don't. Your function simply takes a Month as a parameter. No need
> to check for range. Your caller would have to invoke undefined behaviour
> to break your function then.


If an enum contains values 1...11, then it can also hold values 12...15
without causing undefined behaviour. This language rule exists to allow
'flag' enums, eg:

enum flags
{
Foo = 0x80,
Bar = 0x40,
Quz = 0x20,
};

flags f = Foo | Quz; // 0xA0 is not an enum member, but is valid

So you still have to check range (> 0 as well as <= 12)
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      07-22-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Old Wolf) wrote in
news:(E-Mail Removed) om:

> Andre Kostur <(E-Mail Removed)> wrote:
>
> [snip - debate over whether to use 'enum' for month names]
>
>> No you don't. Your function simply takes a Month as a parameter. No
>> need to check for range. Your caller would have to invoke undefined
>> behaviour to break your function then.

>
> If an enum contains values 1...11, then it can also hold values
> 12...15 without causing undefined behaviour. This language rule exists
> to allow 'flag' enums, eg:


I stand corrected. The enum may contain values beyond the specified ones.

I guess, for me, that would leave it in the realm of "bad programming
practice". One would need to explicitly do something to put the enum out
of range (explicit construction with an out of range value, or do some sort
of bitwise or arithmetic operations on the enums)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What do we call things like Class, Interface, Enumerations etc Chanchal Java 4 11-29-2005 04:36 PM
Help - Using enumerations in a case statement exquisitus Java 2 04-24-2005 10:22 PM
#defines and enumerations in Java - How ? exquisitus Java 12 02-19-2005 03:27 PM
XML Schema - How to do a key-value map with 2 enumerations? Joyce XML 1 02-18-2005 07:43 PM
HttpStatusCode Enumerations... clintonG ASP .Net 4 05-06-2004 02:39 PM



Advertisments