how-to overload conversion operator EMyEnum -> bool

F

Florian Kaufmann

I have an enum with one 'active' element and multiple 'inactive'
elements

enum EActive { eActive, eInactive1, eInactive2 };

I'd like to be able to use EActive values as booleans, where true
means active and false any of the inactive. So the standard enum/int -
bool conversion does not work because eActive would result in false
and the inactive to true. I want that because it results in pretty
readable code.

EActive IsActive();
if ( IsActive() ) { ... }

I fear I cannot overload the conversion operator enum (namly my
EActive) to bool. Is that true?

I do not want to define the enum EInActive { eActive, eInActive1,
eInActive }, because I find the resulting code less readable because
it often has double negation

EInActive IsInActive();
if ( ! IsInActive() ) { ... }

So the only solution (?) is to define a new class somewhat like this.
One would then normally always use CActive instead of EActive.
class CActive {
CActive(const EActive&);
operator bool();
operator==(const EActive&);
operator==(const CActive&);
CActive& operator=(const EActive&);
private:
EActive mData;
};
 
V

Victor Bazarov

I have an enum with one 'active' element and multiple 'inactive'
elements

enum EActive { eActive, eInactive1, eInactive2 };

I'd like to be able to use EActive values as booleans, where true
means active and false any of the inactive.

So, essentially you need to hide this logic:

if (<someExpressionYieldingEActive> == eActive)

or

if (<someExpressionYieldingEActive> != eActive)

, yes? Why do you think it is important to hide?
So the standard enum/int -
and the inactive to true. I want that because it results in pretty
readable code.

EActive IsActive();
if ( IsActive() ) { ... }

I fear I cannot overload the conversion operator enum (namly my
EActive) to bool. Is that true?

Most likely. A conversion from an enumeration to bool is intrinsic to
the language, and you're not allowed to change that (like you're not
allowed to provide your own operator* for doubles, for instance). You
could try to make your EActive a *scoped* enumeration, which isn't
covered by the boolean conversions in the language.

enum struct EActive { eActive, eInactive1, eInactive2 }; // scoped

That requires you to prepend any enumerator with its name, like

EActive::eActive

when used. But this is C++11. I don't see lots of support of it yet.
I do not want to define the enum EInActive { eActive, eInActive1,
eInActive }, because I find the resulting code less readable because
it often has double negation

EInActive IsInActive();
if ( ! IsInActive() ) { ... }

So the only solution (?) is to define a new class somewhat like this.
One would then normally always use CActive instead of EActive.
class CActive {
CActive(const EActive&);
operator bool();

operator bool() const; // most likely
operator==(const EActive&);

bool operator==(const EActive&) const;
operator==(const CActive&);

bool operator==(const CActive&) const;
CActive& operator=(const EActive&);
private:
EActive mData;
};

While the compiler vendors implement scoped enumerations, this is not a
bad choice.

Another possibility is to define another function, name it 'myActive()'
with the signature like

bool myActive(EActive);

and let it convert to bool according to your rules. You'll use it
together with your 'IsActive()':

if (myActive(IsActive())) ...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top