Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > To const or to enum?

Reply
Thread Tools

To const or to enum?

 
 
Conrad Weyns
Guest
Posts: n/a
 
      08-21-2004

"Niels Dekker - no reply address" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Conrad Weyns wrote:
> >
> > Here is what I try to adhere to when using enums:
> >
> > 1. Prefix every enum member with the enum name unless the enum
> > is part of a class in which case it "can" be left out:
> >
> > enum EColorType
> > {
> > EColTypeInvalid,
> > EColTypeRed,
> > EColTypeBlue
> > };

>
>
> Your prefix "EColType" differs from the enum name, but I guess that's a
> typo...?


Indeed! It's a good job I am not anyone's compiler...


> Still, shouldn't we use a little namespace instead? Like
> this:
>
> namespace Color
> {
> enum Type
> {
> Invalid,
> Red,
> Blue
> };
> };
>


Several years ago I did it this way. These days, if it makes sense to
"group" enums I use a class instead of a namespace.
Namespaces tend to leak into header files, even precompiled headers.
Particularly when they contain entities that are "global" in nature.
I find that the use of :: in code quickly becomes an eye catcher.
EColorRed is unambiguous compared to Color::Red
A matter of code convention in the end, no big deal realy
Regards,
Conrad Weyns

>
> Regards,
>
> Niels Dekker
> www.xs4all.nl/~nd/dekkerware



 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      08-21-2004
"Conrad Weyns" <(E-Mail Removed)> wrote in message
news:NYBVc.2280$(E-Mail Removed)...
<snip>
> 2. Never ever cast an enum member.
> Allways provide a complete (however large) switch to map enum
> members to and from other context appropriate values.
> This mapping is vital because this is the place where published
> values (as read from and written to a data base) are maintained and
> de-coupled from the enum.
>
> 3. Never ever use enum members in loop indexes and loop limits.


I'd feel free to break either of those two rules if the only place they are
broken is in the component that declares the enum. I've even written a
light wrapper around an enum to make it a little more user-friendly, and
inside of it, both of those rules were broken. I figure it's OK as long as
I'm not chasing down loops and casts in various unrelated modules. It may
be harder to take this approach if it will create dependency issues (e.g.
the database layer must have a mapping for an enum that it did not declare,
or another component needs to map to a DB-related enum), but it still might
be possible to work something out.

--
David Hilsee


 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-22-2004
David Hilsee wrote:

> Well, I meant that as a joke to poke fun at those who might find enums
> strange or mildly confusing, but I am surprised that you would say that
> enums without explicitly specified values are not immediately
> recognizable. I learned about enums when I first learned C, I have never
> had to think
> twice about them since then. To me, it is immediately recognizable,
> easily understandable, and very familiar, even though I do not often use
> enums or
> see them used in practice. I guess I just found them very intuitive and
> simple.


It depends on the situation. If it is simply a question of recognizing that
the symbold in the enum are enumerators, they there should be little
problem. If you are looking through code to find where thefollowing
definitions form the IDE are bound in C++, and what their values are, I
believe listing them as static const (or at least with explicit value
assignments for each enumerator) will be more immediately obvious than the
alternative shown below.

const unsigned short INDEX_SIZE_ERR = 1;
const unsigned short DOMSTRING_SIZE_ERR = 2;
const unsigned short HIERARCHY_REQUEST_ERR = 3;
const unsigned short WRONG_DOCUMENT_ERR = 4;
const unsigned short INVALID_CHARACTER_ERR = 5;
const unsigned short NO_DATA_ALLOWED_ERR = 6;
const unsigned short NO_MODIFICATION_ALLOWED_ERR = 7;
const unsigned short NOT_FOUND_ERR = 8;
const unsigned short NOT_SUPPORTED_ERR = 9;
const unsigned short INUSE_ATTRIBUTE_ERR = 10;
// Introduced in DOM Level 2:
const unsigned short INVALID_STATE_ERR = 11;
// Introduced in DOM Level 2:
const unsigned short SYNTAX_ERR = 12;
// Introduced in DOM Level 2:
const unsigned short INVALID_MODIFICATION_ERR = 13;
// Introduced in DOM Level 2:
const unsigned short NAMESPACE_ERR = 14;
// Introduced in DOM Level 2:
const unsigned short INVALID_ACCESS_ERR = 15;
// Introduced in DOM Level 3:
const unsigned short VALIDATION_ERR = 16;
// Introduced in DOM Level 3:
const unsigned short TYPE_MISMATCH_ERR = 17;

This form is far less immediately obvious than the IDL definition:
enum{
INDEX_SIZE_ERR = 1, DOMSTRING_SIZE_ERR, HIERARCHY_REQUEST_ERR,
WRONG_DOCUMENT_ERR,
INVALID_CHARACTER_ERR, NO_DATA_ALLOWED_ERR, NO_MODIFICATION_ALLOWED_ERR,
NOT_FOUND_ERR, NOT_SUPPORTED_ERR, INUSE_ATTRIBUTE_ERR, INVALID_STATE_ERR,
SYNTAX_ERR, INVALID_MODIFICATION_ERR, NAMESPACE_ERR, INVALID_ACCESS_ERR,
VALIDATION_ERR, TYPE_MISMATCH_ERR
}

Even this form is less expressive, though logically (practically) identical
to the IDL:
enum{
INDEX_SIZE_ERR = 1,
DOMSTRING_SIZE_ERR,
HIERARCHY_REQUEST_ERR,
WRONG_DOCUMENT_ERR,
INVALID_CHARACTER_ERR,
NO_DATA_ALLOWED_ERR,
NO_MODIFICATION_ALLOWED_ERR,
NOT_FOUND_ERR,
NOT_SUPPORTED_ERR,
INUSE_ATTRIBUTE_ERR,
INVALID_STATE_ERR,
SYNTAX_ERR,
INVALID_MODIFICATION_ERR,
NAMESPACE_ERR,
INVALID_ACCESS_ERR,
VALIDATION_ERR,
TYPE_MISMATCH_ERR
}

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
 
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
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments