is this usage of enum healthy?

Discussion in 'C++' started by lallous, Apr 14, 2005.

  1. lallous

    lallous Guest

    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
     
    lallous, Apr 14, 2005
    #1
    1. Advertising

  2. 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
     
    Matthias Kaeppler, Apr 14, 2005
    #2
    1. Advertising

  3. lallous

    lilburne Guest

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

    }
     
    lilburne, Apr 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    548
  2. Jerminia
    Replies:
    3
    Views:
    633
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    468
    Rolf Magnus
    Nov 13, 2003
  4. Irving Fried
    Replies:
    3
    Views:
    597
    Manish Pandit
    Sep 10, 2006
  5. Foodmatch

    Healthy Mediterranean Diet

    Foodmatch, Nov 30, 2009, in forum: C++
    Replies:
    1
    Views:
    326
    Mediterranean Diet .TV
    Nov 30, 2009
Loading...

Share This Page