Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Composite Assignment Operators for enums

Reply
Thread Tools

Composite Assignment Operators for enums

 
 
David Rasmussen
Guest
Posts: n/a
 
      02-24-2004
Given an enum type E, how do I make, say, operator += work for that enum
type?

/David
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-24-2004
* David Rasmussen <(E-Mail Removed)> 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;
}

 
Reply With Quote
 
 
 
 
David Rasmussen
Guest
Posts: n/a
 
      02-24-2004
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
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      02-24-2004
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-lit...html#faq-29.18
http://tinyurl.com/28ntu
http://cpptips.hyperformix.com/Enums.html
http://www.bearcave.com/software/jav...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

 
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
Enums without identifier, enums and typedef =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= C Programming 10 01-20-2007 01:20 AM
Losing Composite Control property that another Composite Control ... Chad ASP .Net Building Controls 0 02-01-2005 09:01 PM
Overloading composite operators? bq C++ 2 08-13-2004 01:17 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
Composite control with dynamic composite controls sleigh ASP .Net 1 02-12-2004 06:24 PM



Advertisments