Composite Assignment Operators for enums

Discussion in 'C++' started by David Rasmussen, Feb 24, 2004.

  1. Given an enum type E, how do I make, say, operator += work for that enum
    type?

    /David
     
    David Rasmussen, Feb 24, 2004
    #1
    1. Advertising

  2. * David Rasmussen <> schriebt:
    >
    > Given an enum type E, how do I make, say, operator += work for that enum
    > type?


    Best to avoid enum types.

    C++ is not Ada, as we have discussed before a few times... ;-)

    But if you absolutely must,


    E& operator += (E& lhs, E rhs )
    {
    lhs = static_cast<E>( lhs + rhs );
    return lhs;
    }
     
    Alf P. Steinbach, Feb 24, 2004
    #2
    1. Advertising

  3. Alf P. Steinbach wrote:
    >
    > Best to avoid enum types.
    >


    I disagree. To me, it's like saying "it's best to avoid ints".

    > C++ is not Ada, as we have discussed before a few times... ;-)
    >


    But hopefully, C++ will learn something from Ada as time goes on.

    > But if you absolutely must,
    >
    >
    > E& operator += (E& lhs, E rhs )
    > {
    > lhs = static_cast<E>( lhs + rhs );
    > return lhs;
    > }
    >


    Thanks!

    /David
     
    David Rasmussen, Feb 24, 2004
    #3
  4. David Rasmussen wrote:

    > Given an enum type E, how do I make, say, operator += work for that enum
    > type?
    >
    > /David


    By converting the enum into a class or providing global functions
    to perform the operation.

    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.18
    http://tinyurl.com/28ntu
    http://cpptips.hyperformix.com/Enums.html
    http://www.bearcave.com/software/java/misl/enum/type_safe_enum.html

    You can get a lot of examples by typing "enumeration C++" into
    your favorite search engine.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Feb 24, 2004
    #4
    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. sleigh
    Replies:
    1
    Views:
    2,737
    sleigh
    Feb 12, 2004
  2. bq
    Replies:
    2
    Views:
    757
    Victor Bazarov
    Aug 13, 2004
  3. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,193
    Keith Thompson
    Jan 20, 2007
  4. Chad
    Replies:
    0
    Views:
    253
  5. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    292
    Sundararajan
    Mar 11, 2005
Loading...

Share This Page