extending enum's: what operators?

Discussion in 'C++' started by Simon Elliott, Jan 9, 2006.

  1. What operators can be used to extend an enum?

    Can I extend an enum with an operator= and if so what's the syntax?

    --
    Simon Elliott http://www.ctsn.co.uk
    Simon Elliott, Jan 9, 2006
    #1
    1. Advertising

  2. Simon Elliott

    Rolf Magnus Guest

    "Simon Elliott" <Simon at ctsn.co.uk> wrote:

    > What operators can be used to extend an enum?


    What do you mean by "extend and enum"?
    Rolf Magnus, Jan 9, 2006
    #2
    1. Advertising

  3. On 09/01/2006, Rolf Magnus wrote:

    > > What operators can be used to extend an enum?

    >
    > What do you mean by "extend and enum"?


    For example:

    enum garden_veg {CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08};
    garden_veg operator|(garden_veg lhs, garden_veg rhs)
    {
    return(garden_veg(lhs|rhs));
    }

    ....

    garden_veg ct = CARROT|TURNIP;

    I've seen operator|, operator&, and (postfix and prefix) operator++ and
    operator--, but I haven't seen operator=.

    --
    Simon Elliott http://www.ctsn.co.uk
    Simon Elliott, Jan 9, 2006
    #3
  4. Simon Elliott

    Ben Pope Guest

    Simon Elliott wrote:
    > On 09/01/2006, Rolf Magnus wrote:
    >
    >>> What operators can be used to extend an enum?

    >> What do you mean by "extend and enum"?

    >
    > For example:
    >
    > enum garden_veg {CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08};
    > garden_veg operator|(garden_veg lhs, garden_veg rhs)
    > {
    > return(garden_veg(lhs|rhs));
    > }
    >
    > ...
    >
    > garden_veg ct = CARROT|TURNIP;
    >
    > I've seen operator|, operator&, and (postfix and prefix) operator++ and
    > operator--, but I haven't seen operator=.


    garden_veg(CARROT|TURNIP)

    CARROT is converted to an int, TURNIP is converted to an int. The
    result is 3, 3 is undefined for garden_veg.

    When initialising an enum from an int, you need to cast:
    garden_veg v(static_cast<garden_veg>(CARROT|TURNIP));

    The value in v is now undefined (it might be 1, 2, 3 or anything else),
    since it doesn't represent any value of garden_veg.

    Perhaps you need to describe the problem you wish to solve, since you
    seem to have a fundamental problem with what you are trying to represent.

    You should probably have:

    typedef unsigned int veg_mask;

    veg_mask dinner = CARROT | TURNIP;

    if (dinner & CARROT) {
    std::cout << "meal contains beta carotene" << std::endl;
    }



    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Jan 9, 2006
    #4
  5. On 09/01/2006, Ben Pope wrote:
    > You should probably have:
    >
    > typedef unsigned int veg_mask;


    Yes, although this still has problems because veg_mask has no type
    safety. But short of wrapping the enum in a class I'm not sure there's
    a better way. (I'm interested in putting together some kind of template
    code as an alternative to typedef of PODs generally, but that's a whole
    other thread.)

    The operator| was just an example to answer Rolf Magnus's question. As
    per my original post, it's the operator= I'm really interested in.


    --
    Simon Elliott http://www.ctsn.co.uk
    Simon Elliott, Jan 9, 2006
    #5
  6. Simon Elliott

    Ben Pope Guest

    Simon Elliott wrote:
    > On 09/01/2006, Ben Pope wrote:
    >> You should probably have:
    >>
    >> typedef unsigned int veg_mask;

    >
    > Yes, although this still has problems because veg_mask has no type
    > safety


    Indeed.

    > But short of wrapping the enum in a class I'm not sure there's
    > a better way.


    I don't think so either. An enum is essentially an int, with redefined
    values.

    > The operator| was just an example to answer Rolf Magnus's question. As
    > per my original post, it's the operator= I'm really interested in.


    Well, you can't override the operators of an int or enum, you'll have to
    use your own type (class/struct).

    I think boost has some kind of extended enum, it was discussed on the
    list, anyway:
    http://lists.boost.org/Archives/boost/2005/12/97685.php

    Perhaps there's something there that's useful.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Jan 9, 2006
    #6
  7. Ben Pope wrote:

    > Well, you can't override the operators of an int or enum, you'll have to
    > use your own type (class/struct).


    With respect to an enum, ITYM you can't override operator=() (since it
    must be a member function). You can, of course, override other
    operator functions that do not need to be a member function (e.g.,
    operator+(), operator++, operator|(), operator<<(), etc.).

    Best regards,

    Tom
    Thomas Tutone, Jan 9, 2006
    #7
  8. Simon Elliott

    Pete Becker Guest

    Ben Pope wrote:
    >>
    >> enum garden_veg {CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08};

    >
    > When initialising an enum from an int, you need to cast:
    > garden_veg v(static_cast<garden_veg>(CARROT|TURNIP));
    >
    > The value in v is now undefined (it might be 1, 2, 3 or anything else),
    > since it doesn't represent any value of garden_veg.
    >


    C++ enums differ from those in C in exactly that regard. If the numeric
    value you're assigning fits in the same number of bits as the enum (in
    this case, 4) the result is well defined. That's so you can do exactly
    this sort of bit tweaking. The overloaded operator| should be declared
    to return a garden_veg, and as long as there aren't any silly mistakes
    in the code, the result will work just fine.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jan 9, 2006
    #8
    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. Roedy Green

    extending enum

    Roedy Green, Jul 31, 2005, in forum: Java
    Replies:
    18
    Views:
    36,873
    chrismay
    Jun 25, 2011
  2. Extending Enum

    , May 15, 2007, in forum: Java
    Replies:
    3
    Views:
    962
  3. Roedy Green

    Extending an enum

    Roedy Green, Jul 16, 2007, in forum: Java
    Replies:
    4
    Views:
    696
    Roedy Green
    Jul 16, 2007
  4. Christopher

    deriving a class - extending enum

    Christopher, Dec 5, 2007, in forum: C++
    Replies:
    15
    Views:
    1,233
    Roland Pibinger
    Dec 6, 2007
  5. Laura Schmidt
    Replies:
    8
    Views:
    94
    Roedy Green
    Jun 16, 2014
Loading...

Share This Page