C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

overloading enumerated types

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
KublaPawn
Guest





PostPosted: Wed Jul 21, 2004 11:48 am    Post subject: overloading enumerated types Reply with quote



Is it possible to overload an enumerated type.for example:

enum square{a1,b1,c1 ... f8,g8,h8};


and use a1++; to get from one square to another.
and a1+=8; to get to another row;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Wed Jul 21, 2004 7:11 pm    Post subject: Re: overloading enumerated types Reply with quote



In article <0AhLc.19072$Gf7.669224 (AT) news20 (DOT) bellglobal.com>, KublaPawn
<KublaPawn (AT) netscape (DOT) net> writes
Quote:
Is it possible to overload an enumerated type.for example:

enum square{a1,b1,c1 ... f8,g8,h8};


and use a1++; to get from one square to another.
and a1+=8; to get to another row;

Yes. Whether it is desirable or a good design is an entirely different
issue.

Example:

square operator++(square val, int){
square temp(square(val+1));
if(temp == hCool temp = a1;
return temp;
}

I have made no attempt at polishing the above. I am just providing an
existence proof.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Jul 21, 2004 7:13 pm    Post subject: Re: overloading enumerated types Reply with quote



KublaPawn wrote:
Quote:
Is it possible to overload an enumerated type.for example:

enum square{a1,b1,c1 ... f8,g8,h8};


and use a1++; to get from one square to another.
and a1+=8; to get to another row;

Yes, you can overload operators for enumerations.
No, the expressions 'a1++' and 'a1+=8' will never make sense, as 'a1' is a
constant. 'square s = a1; s++;' makes sense though.

Uli

--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Wed Jul 21, 2004 8:03 pm    Post subject: Re: overloading enumerated types Reply with quote

KublaPawn wrote:
Quote:
Is it possible to overload an enumerated type.for example:

enum square{a1,b1,c1 ... f8,g8,h8};


and use a1++; to get from one square to another.
and a1+=8; to get to another row;

Yes, partially. The term is "overload some operators for an enumerated
type", not "overload an enumerated type".

You should be able to overload the post-increment, but not the compound
assignment.

square operator++(square& s, int) {
square r = s;
// here you define how to move from one to another
// and change 's' accordingly
return r;
}

The assignment operator has to be a member, and since enums are not
classes, they can't have members.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.