473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

extending enum's: what operators?

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
Jan 9 '06 #1
7 6534
"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"?

Jan 9 '06 #2
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
Jan 9 '06 #3
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...
Jan 9 '06 #4
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
Jan 9 '06 #5
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...
Jan 9 '06 #6

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

Jan 9 '06 #7
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)
Jan 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
8
by: Imran | last post by:
Hello All I have a enum decla like this enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well monday, ...
7
by: bberu | last post by:
Hi, I know it is possible to refine operators (like +, -, * or /) on classes. But is it possible on simple types ? ex : If I have a type like : typedef int vector is it possible to redefine a...
5
by: John Smith | last post by:
I have a custom enum to list comparison operators: =, <, <=, >, >= . I have created it as follows: public enum Comparator {Equal, LessThan, LessThanOrEqual,GreaterThan,GreaterThanOrEqual} Now...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
1
by: Vincent RICHOMME | last post by:
Hi, I would like to know what is the best way of doing the following thing. I am developping a GUI application with a tree and for each item of my tree I can associate(gui mechanism) a class to...
27
by: Randy | last post by:
Is there a way to override operators for user-defined types (e.g., typedefs) rather than class types? I'm trying to override the extractor operator for a user-defined enumeration type but...
2
by: Stipanicev | last post by:
I want to write generic operators for matrix computation which would adapt to different types (shapes) of matrices i.e. when adding square matrix and lower triangle matrix it would add only...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.