Setting Up Option Parameters :: C++

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi.

I would like to create a generic class that wraps around, say, an API function. I need a way to pass into a function parameters such as FEATURE1 | FEATURE3. I believe that requires the use of bitwise AND, OR, and NOT similar to MFC programming style.

For example:

-----
// call function to initialize variables

SetVariableA(FEATURE1 | FEATURE 3) // I want feature1 and feature3
-----

Here is what SetVariableA(...) might look like

-----
enum STYLE {FEATURE1 = 0, FEATURE2, FEATURE3}

SetVariableA(...)
{
}
-----

Basically, I am not familiar with the use of enum with bitwise AND, OR, and NOT. Here is a what I would like to accomplish.

- user calls function to setup a feature: function(FEATURE1)
- function determines what feature the user wants

How would you implement some like the above using enum, bitwise AND, OR, and NOT?

Thanks,
Kuphryn

P.S. MFC programmers seem
 

KevinMU1

Senior member
Sep 23, 2001
673
0
0
you can use define to make your constants in powers of 2

#define FEATURE1 01
#define FEATURE2 02
#define FEATURE3 04
#define FEATURE4 08

then, in your method:

void mymeth(int features)
{
if (features & FEATURE1)
{
}
if (features & FEATURE2)
{
}
}

the bitwise statement will only be true if the result is non-zero, and for each bit position it will only be nonzero if it's defined in both the feature set requested and the individual feature that you are checking against.

Let me know if that's not detailed enough.