Article View: comp.std.c++
Article #3663Re: String value of enum
From: Max Welton
Date: Thu, 14 Mar 1996 04:18
Date: Thu, 14 Mar 1996 04:18
31 lines
2279 bytes
2279 bytes
mtm4@rsvl.unisys.com (Michael McCormick) wrote:
>"W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:
>
>>The system that I'm working often needs to know the string
>>value of an enum. e.g. enum week{MON=1, ..., SUN} it; it =
>>TUE;
>>Then when this enum is passed as a parameter the function
>>should return the string based on the enum. For instance,
>>weekImage(it) will return "TUE". Is it not possible that such
>>a functionality can be built into enums?
>
>What you are essentially asking for is language support for converting
>a label into a string respresentation of its name. That is a very
>unusual feature to find in any language, since it would require the
>compiler to place the symbolic dictionary in the executable file. I
>don't think there's a snowball's chance of this getting into C++.
It's not _that_ unusual. Ada has had this feature starting with
Ada-83 (the 'image attribute).
Actually, I've done the above in C++ by wrapping the enum of
interest in a class, placing a static array of const strings
in the implementation-file and providing an accessor member
function that simply indexes into the array and returns the
appropriate string (or a const char* to it, anyhow).
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<4i80om$2jo@ixnews3.ix.netcom.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>Show Full Headers
Complete Article Headers
From: Max Welton <maxw@ix.netcom.com> Message-ID: <4i80om$2jo@ixnews3.ix.netcom.com> X-Original-Date: 14 Mar 1996 02:37:10 GMT Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 14 Mar 96 04:18:08 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: String value of enum Organization: YoYoDyne References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com> X-Netcom-Date: Wed Mar 13 6:37:10 PM PST 1996 X-Mailer: Mozilla 1.22 (Windows; I; 16bit) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMUeeDeEDnX0m9pzZAQFxNwF+NKucH+Z2uoVPAvoKoiq/NfkasRyK7OyA Q5rhGoPZgOO1LnjO4WRkc8Rzo6Jv4W+u =Up3a