home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19775 < prev    next >
Encoding:
Text File  |  1993-01-23  |  2.2 KB  |  59 lines

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Subject: Re: smart enums
  5. Reply-To: nikki@trmphrst.demon.co.uk
  6. References: <1993Jan21.054036.27343@hparc0.aus.hp.com>
  7. Distribution: world
  8. X-Mailer: cppnews $Revision: 1.31 $
  9. Organization: Trumphurst Ltd.
  10. Lines: 44
  11. Date: Fri, 22 Jan 1993 15:53:06 +0000
  12. Message-ID: <727743186snx@trmphrst.demon.co.uk>
  13. Sender: usenet@demon.co.uk
  14.  
  15. In article <1993Jan21.054036.27343@hparc0.aus.hp.com> peterj@hparc0.aus.hp.com (Peter Westley) writes:
  16. > I am trying to create a class which implements a "smart enum".  This enum
  17. > type will be able recognise itself as both an int (or other integral type)
  18. > or as char* and also have stndard conversions to both int and char* for
  19. > things sunch as stream insertion.
  20. > The way I am trying to implement them is with templates.  Say I wanted
  21. > an enum class type which could take the values "Apple", "Banana" and "Pear"
  22. > each assigned (an arbitary) int.  Basically to bahave much as an enum normally
  23. > does.
  24. ..
  25. > The idea being that I could declare a class based on this template which
  26. > I could have multiple instances of:
  27. >     char *somenames[] = {"Apple","Banana","Pear"};
  28. >     char *morenames[] = {"Peter","John","Andrew"};
  29. >     typedef SmartEnum<somenames> FruitEnum;
  30. >     typedef SmartEnum<morenames> BoysEnum;
  31. >     FruitEnum aFruit();
  32. >     FruitEnum anotherFruit();
  33. >     BoysEnum aBoy();
  34. >     BoysEnum anotherBoy();
  35. > Then I could do things like:
  36. >     aFruit = "Banana";
  37. >     anotherFruit = 1;
  38. You don't want to do that, surely ! The whole point of enums is that you 
  39. can't convert arbitrary integers to them (but you can convert them to 
  40. integers).
  41.  
  42. You might want to have increment and decrement operators (provided you 
  43. could test at runtime whether they had been successful).
  44.  
  45. If you output these enums to a stream in one format more often than the 
  46. other, you might want to have an operator<< which used the default format,
  47. rather than always having to use a cast.
  48.  
  49. Input operators are another possibility (checking for valid values, of 
  50. course).
  51.  
  52. -- 
  53. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  54. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  55.