Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is this usage of enum healthy?

Reply
Thread Tools

is this usage of enum healthy?

 
 
lallous
Guest
Posts: n/a
 
      04-14-2005
Hello

I sometimes convert lots of numeric defines to enum, as:

#define XYZ 0x12345

to

enum some_consts
{
XYZ = 0x1234
};

Sometimes one enum, is used as:

enum some_consts
{
XYZ1 = 0x1234,
X1 = 1,
X2 = 2,
Y1 = 1,
Y2 = 2
};

etc...too many duplicate values, no order is used, etc...is that okay?

Or I create a namespace and use const int definitions inside?

Regards,
Elias


 
Reply With Quote
 
 
 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      04-14-2005
lallous wrote:
> Hello
>
> I sometimes convert lots of numeric defines to enum, as:
>
> #define XYZ 0x12345
>
> to
>
> enum some_consts
> {
> XYZ = 0x1234
> };
>
> Sometimes one enum, is used as:
>
> enum some_consts
> {
> XYZ1 = 0x1234,
> X1 = 1,
> X2 = 2,
> Y1 = 1,
> Y2 = 2
> };
>
> etc...too many duplicate values, no order is used, etc...is that okay?
>
> Or I create a namespace and use const int definitions inside?
>
> Regards,
> Elias
>
>


I don't think this will break your program or so, but it's just not what
enums are intendend to use for. As you said, namespaces are your friends:

namespace my_const_data
{
const int BAR = 1;
const int FOO = 0x22;
// ...
}

--
Matthias Kaeppler
 
Reply With Quote
 
 
 
 
lilburne
Guest
Posts: n/a
 
      04-14-2005


lallous wrote:

> Hello
>
> I sometimes convert lots of numeric defines to enum, as:
>
> #define XYZ 0x12345
>
> to
>
> enum some_consts
> {
> XYZ = 0x1234
> };
>
> Sometimes one enum, is used as:
>
> enum some_consts
> {
> XYZ1 = 0x1234,
> X1 = 1,
> X2 = 2,
> Y1 = 1,
> Y2 = 2
> };
>
> etc...too many duplicate values, no order is used, etc...is that okay?
>
> Or I create a namespace and use const int definitions inside?
>


In general I wouldn't use enums to introduce a set of value constants
unless the set was cohesive and the values had to map directly into the
problem domain. In most cases I'll use single value enums as in

enum utilsCopy {
utilsCOPY
};

enum geomOrigin {
geomORIGIN
};

enum geomXaxis {
geomXAXIS
};

and use them as in:

class Vector {
public:
Vector(geomXaxis);
Vector(geomYaxis);
Vector(geomZaxis);
};

class Point {
public:
Point(geomOrigin);
}

class myClass {
public:
myClass(utilsCopy, const myClass& a);
};


void some_method(const myClass& arg)
{

Vector v(geomXAXIS);
myClass c(utilsCOPY, arg);

}


 
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
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments