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 -
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;
};
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 -
and the inactive to true. I want that because it results in prettybool conversion does not work because eActive would result in false
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;
};