how-to overload conversion operator EMyEnum -> bool

Discussion in 'C++' started by Florian Kaufmann, Feb 17, 2012.

  1. 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;
    };
     
    Florian Kaufmann, Feb 17, 2012
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Feb 17, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  2. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    597
    Rob Williscroft
    Mar 5, 2004
  3. Chiller

    bool overload problem

    Chiller, Apr 11, 2004, in forum: C++
    Replies:
    2
    Views:
    315
    John Harrison
    Apr 11, 2004
  4. Replies:
    2
    Views:
    427
    Alf P. Steinbach
    Dec 3, 2005
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    260
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page