extending enum's: what operators?

S

Simon Elliott

What operators can be used to extend an enum?

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

Simon Elliott

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=.
 
B

Ben Pope

Simon said:
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
 
S

Simon Elliott

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.
 
B

Ben Pope

Simon said:
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
 
T

Thomas Tutone

Ben said:
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
 
P

Pete Becker

Ben said:
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top