is this usage of enum healthy?

L

lallous

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
 
M

Matthias Kaeppler

lallous said:
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;
// ...
}
 
L

lilburne

lallous said:
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);

}
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top