Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how-to overload conversion operator EMyEnum -> bool

Reply
Thread Tools

how-to overload conversion operator EMyEnum -> bool

 
 
Florian Kaufmann
Guest
Posts: n/a
 
      02-17-2012
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;
};
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-17-2012
On 2/17/2012 1:04 PM, Florian Kaufmann wrote:
> 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 -
>> 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?


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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implicit conversion of bool to int tim@DELETEMErobotcrazy.com C++ 2 12-03-2005 04:52 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
bool overload problem Chiller C++ 2 04-11-2004 06:16 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57