By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

is this usage of enum healthy?

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a


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);

}
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.