Dayan Dayan - 1 month ago 5
C++ Question

C++ Multiple enum elements being passed as parameter?

I've been trying to figure this portion of C++ code I'm converting over to JAVA. The method call

PrintItem()
takes in
DWORD
as a second parameter, but in this particular example it allows two elements from the
enum
at once?

I was refering to the following answer by Ashish to try and get some clarification but it didn't help much.

enum
{
PRINT_ENTRY = 0x0001,
PRINT_PREPEND = 0x0002,
PRINT_APPEND = 0x0004,
PRINT_PRECOMMENTS = 0x0008,
PRINT_POSTCOMMENTS = 0x0010,
PRINT_SPECIAL = 0x0010,
PRINT_DEFAULT = PRINT_ENTRY | PRINT_PREPEND | PRINT_APPEND | PRINT_PRECOMMENTS | PRINT_POSTCOMMENTS | PRINT_SPECIAL
};


The method call with two enum elements being passed in? This is what I don't understand.

bResult = PrintItem( pMergeItem, PRINT_PREPEND | PRINT_ENTRY );


Method declaration

bool PrintItem( CItem* pItem, DWORD options = PRINT_DEFAULT );


And the Definition, trimmed down a bit:

// Prints an individual item to the invoice (based on appropriate template settings)
bool CPrintRptInvoice::PrintItem( CItem* pItem, DWORD options )
{
bool bResult = true;
CString strKey = GetItemKey( pItem );

DWORD getOpts = CPrintTemplate::GE_NONE;
if ( options & PRINT_ENTRY )
getOpts |= CPrintTemplate::GE_ENTRY;
if ( options & PRINT_PREPEND )
getOpts |= CPrintTemplate::GE_PREPEND;
if ( options & PRINT_APPEND )
getOpts |= CPrintTemplate::GE_APPEND;

CString strEntry = m_pTemplate->GetEntry( strKey, getOpts );

// print any prefix first
if ( options & PRINT_PRECOMMENTS )
PrintComment( pItem->GetInvoicePrefix() );

// print main entry
if ( strEntry.IsEmpty() )
{
if ( ( options & PRINT_ENTRY ) && !m_pTemplate->EntryExists( strKey ) )
{
//Code to print Entries, not touching options anymore.
}
}

// print any special cases
if ( bResult && ( options & PRINT_SPECIAL ) )
{
//Code to print special cases, not touching options anymore...
}

// print any suffix last
if ( options & PRINT_POSTCOMMENTS )
PrintComment( pItem->GetInvoiceSuffix() );

return bResult;
}


My question is - How is it dealing with multiple parameters of enum? I don't see it handling any of that in that method definition! What i trimmed out doesn't touch
options
at all.

I guess I'm confused because I see
options
as a collection of some sort, no? So, how is it that you can do this and just get the right element:
if ( options & PRINT_PRECOMMENTS )
..

Answer

That is just a bitwise OR, which allows to combine more than one enum value into the integer which is is, internally (so still just one argument in the function call). Such enums are also called a flagset. They are usually characterized by members that have values which represent just one bit: a = 1, b = 2, c = 4, d = 8, e = 16, ... so they can be combined; a | b is 3 when casted to int. Inside the function body, individual flags are then queried separately using the bitwise & operator.

additional reference: How to use enums as flags in C++?

btw. this concept seems to be quite similar in Java: Implementing a bitfield using java enums

Comments