473,770 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using the OR | w/ an enum

So I have been doing C++ for quite awhile but have never had too many
occasions to use |.

What I'm trying to provide is ability to say enum myEnum = myEnum::one
| myEnum::two.

I'm not sure if this makes sense so I'll use my actual situation as an
example.

I am doing a menu system where each button the screen has an access
role as does the user. So the simplest example would be.

User is Susie;

Button 1 should be available to Susie or Johnny.

So Button1.Access = myEnum::Susie | myEnum::Johnny.

Is that the best way to do it? Is it even possible? Thanks for the
help.

Sep 14 '07 #1
4 1803
On Sep 14, 10:07 am, Travis <travis.bow...@ gmail.comwrote:
So I have been doing C++ for quite awhile but have never had too many
occasions to use |.

What I'm trying to provide is ability to say enum myEnum = myEnum::one
| myEnum::two.

I'm not sure if this makes sense so I'll use my actual situation as an
example.

I am doing a menu system where each button the screen has an access
role as does the user. So the simplest example would be.

User is Susie;

Button 1 should be available to Susie or Johnny.

So Button1.Access = myEnum::Susie | myEnum::Johnny.

Is that the best way to do it? Is it even possible? Thanks for the
help.

Oh I forgot to add that I got this idea that using | might work
because I didn't want to have to go through and do all the different
combinations like myEnum::Susie, myEnum::Johnny,
myEnum::JohnnyA NDSusie, etc.

Sep 14 '07 #2
Travis wrote:
On Sep 14, 10:07 am, Travis <travis.bow...@ gmail.comwrote:
>So I have been doing C++ for quite awhile but have never had too many
occasions to use |.

What I'm trying to provide is ability to say enum myEnum =
myEnum::one
>>myEnum::two .
In most cases you cannot do that (nor do you really want to). If you
think that the inclusive-or combination of those two values should
also be a valid value in your 'myEnum', add it there *explicitly* in
the definition of the 'myEnum' enumeration.
>I'm not sure if this makes sense so I'll use my actual situation as
an example.

I am doing a menu system where each button the screen has an access
role as does the user. So the simplest example would be.

User is Susie;

Button 1 should be available to Susie or Johnny.

So Button1.Access = myEnum::Susie | myEnum::Johnny.

Is that the best way to do it? Is it even possible? Thanks for the
help.


Oh I forgot to add that I got this idea that using | might work
because I didn't want to have to go through and do all the different
combinations like myEnum::Susie, myEnum::Johnny,
myEnum::JohnnyA NDSusie, etc.
It would work if you don't try to make 'Button1.Access ' of the type
'myEnum'. It should be of type 'int' or 'unsigned' (or some such)
and when you need to check if 'Johnny' has access you would also do
the 'and' operation:

myEnum john_smith = Johnny; //
...
if (john_smith & Button1.Access) // Does 'john_smith' have access?
...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 14 '07 #3
On Sep 14, 7:07 pm, Travis <travis.bow...@ gmail.comwrote:
So I have been doing C++ for quite awhile but have never had too many
occasions to use |.
What I'm trying to provide is ability to say enum myEnum = myEnum::one
| myEnum::two.
I'm not sure if this makes sense so I'll use my actual situation as an
example.
I am doing a menu system where each button the screen has an access
role as does the user. So the simplest example would be.
User is Susie;
Button 1 should be available to Susie or Johnny.
So Button1.Access = myEnum::Susie | myEnum::Johnny.
Is that the best way to do it? Is it even possible?
You have to define the enum values as bit masks, e.g.:

enum E
{
Susie = 0x01,
Johnny = 0x02,
// ...
} ;

You can then overload the | and the |= operators:

E
operator|( E lhs, E rhs )
{
return static_cast< E >(
static_cast< unsigned >( lhs )
| static_cast< unsigned >( rhs ) ) ;
}

E operator|=( E& lhs, E rhs )
{
lhs = lhs | rhs ;
}

If you do this, you may want to add an enum value none = 0,
since you can't pass 0 as an argument to a function expecting an
E. (Note that this sort of thing is a very frequent use of
enums in C++.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 15 '07 #4
On Sep 14, 7:15 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Travis wrote:
On Sep 14, 10:07 am, Travis <travis.bow...@ gmail.comwrote:
So I have been doing C++ for quite awhile but have never had too many
occasions to use |.
What I'm trying to provide is ability to say enum myEnum =
myEnum::one
myEnum::two.
In most cases you cannot do that (nor do you really want to).
Nonsense. The standard does it in many cases: ios::state, for
example.
If you think that the inclusive-or combination of those two
values should also be a valid value in your 'myEnum', add it
there *explicitly* in the definition of the 'myEnum'
enumeration.
That's not always reasonable, if the number of cases is large.

[...]
Oh I forgot to add that I got this idea that using | might work
because I didn't want to have to go through and do all the different
combinations like myEnum::Susie, myEnum::Johnny,
myEnum::JohnnyA NDSusie, etc.
It would work if you don't try to make 'Button1.Access ' of the
type 'myEnum'.
It works very well if Button1.Access has an enumerated type as
well. I do it all the time.
It should be of type 'int' or 'unsigned' (or some such)
and when you need to check if 'Johnny' has access you would also do
the 'and' operation:
myEnum john_smith = Johnny; //
...
if (john_smith & Button1.Access) // Does 'john_smith' have access?
...
You can apply the built-in | or & directly to the enum type, but
the results will have the underlying integral type (and that
should be:
if ( (john_smith & Button1.Access) != 0 )
unless you're into obfuscation). But since enum's are user
defined types, you can also overload the |, & and ~ operators
for them, to get the correct types. (Another alternative would
be to overload + and -, to add or remove an element, and use a
function contains() to test whether the enum contained the
element.)

Despite their name, enum, in C++, is not necessarily an
enumerated type, in the classical sense. It can be used as
such, but it can also be used as a small set of named elements.
Such use is very idiomatic C++.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 15 '07 #5

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

Similar topics

11
6600
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
1
2760
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many colors are referred in different subsystems in our projects. They are defined as enumeration constants and a single color must be the same value all across our projects.
3
4266
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
6
28607
by: Brian Haynes | last post by:
I've read all the posts in this forum that I can find that look related to this issue and I have only found 1 solution that I consider to be a bit of a hack. What I want to do is assign a value to an enum variable using an int. What I am using right now is something like this: public enum MyEnum { value1, value2, value3
1
15761
by: Chris Dunaway | last post by:
Suppose I have the following enum: public enum MyEnum { EnumVal1 = 1, EnumVal2 = 2, EnumVal3 = 3 } I want to serialize the enum but I want the /names/ of the enum to be stored in the .xml file, like this:
10
5044
by: Rick Palmer | last post by:
I have an app I'm working on that will allow a user to run one of 5 reports. The report names are in a combobox on my form. I have a sub defined for each report that has the exact same name as is displayed in the combobox. I have one button on the form to start processing. What I want to do is this: When the user selects the report they want to run from the combobox, I want to dynamically bind the appropriate sub to the button's click...
13
12392
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
6
17205
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically, process and output to the screen in my application when a message arrived. But the problem is how do I read the SMS message immediately when it arrived without my handphone BeEPINg for new message ? I read up the AT commands, but when getting down...
7
9837
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
2
1472
by: puzzlecracker | last post by:
I am porting old java code to csharp and now facing a stumbling block. Before advent of enum in Java, developers used enum-like structures, shown below. However, AFAIK, CSharp isn't lacking this particular feature, and I don't want to port it in an old way. Here is the code below (in java ) that I want to approximate in C#. Note that I have lots of classes that follow this sort of principles. public class Derived extends Base {
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.