Archived

This topic is now archived and is closed to further replies.

kuphryn

Setting Up Option Parameters :: C++

Recommended Posts

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 to use the bitwise AND, OR, and NOT

Share this post


Link to post
Share on other sites
it would look a bit like this

  
const int option1 = 1<<1;
const int option2 = 1<<2;
const int option3 = 1<<3;

void dosomething(int flags)
{
if (flags & option1)
{
// do something for option1

}
if (flags & option2)
{
// do something for option 2

}
if (flags & option3)
{
// do something for option 3, you get the idea

}
}


dosomething(option1 | option3);

hope that helps

Share this post


Link to post
Share on other sites
Thanks.

In the code you posted, I understand cases (flags & option1), etc., but how about cases such as OPTION1 | OPTION2?

Kuphryn

Share this post


Link to post
Share on other sites
i believe i might look something like this:

if ((flags & option1) && (flags & option2))

i havent tested it, but i think it should work

Share this post


Link to post
Share on other sites