Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extending enum's: what operators?

Reply
Thread Tools

extending enum's: what operators?

 
 
Simon Elliott
Guest
Posts: n/a
 
      01-09-2006
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-09-2006
"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"?

 
Reply With Quote
 
 
 
 
Simon Elliott
Guest
Posts: n/a
 
      01-09-2006
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
 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      01-09-2006
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...
 
Reply With Quote
 
Simon Elliott
Guest
Posts: n/a
 
      01-09-2006
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
 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      01-09-2006
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...
 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      01-09-2006

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

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-09-2006
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extending the reception of a network yar Wireless Networking 3 06-15-2005 05:16 PM
extending a wireless Stuart Turrell Wireless Networking 6 11-30-2004 10:18 PM
Extending a Vlan Dak991 Cisco 1 11-23-2004 06:48 PM
Extending console cable Stibloc Cisco 3 02-29-2004 02:34 AM
Extending a T1 over Ethernet / Fiber link Rick G Cisco 2 07-11-2003 09:22 PM



Advertisments