home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19670 < prev    next >
Encoding:
Internet Message Format  |  1993-01-21  |  2.4 KB

  1. From: peterj@hparc0.aus.hp.com (Peter Westley)
  2. Date: Thu, 21 Jan 1993 05:40:36 GMT
  3. Subject: smart enums
  4. Message-ID: <1993Jan21.054036.27343@hparc0.aus.hp.com>
  5. Organization: HP Australasian Response Centre (Melbourne)
  6. Path: sparky!uunet!usc!sdd.hp.com!hpscit.sc.hp.com!hplextra!hpcc05!hparc0.aus.hp.com!peterj
  7. Newsgroups: comp.lang.c++
  8. X-Newsreader: Tin 1.1 PL4
  9. Lines: 69
  10.  
  11. I am trying to create a class which implements a "smart enum".  This enum
  12. type will be able recognise itself as both an int (or other integral type)
  13. or as char* and also have stndard conversions to both int and char* for
  14. things sunch as stream insertion.
  15.  
  16. The way I am trying to implement them is with templates.  Say I wanted
  17. an enum class type which could take the values "Apple", "Banana" and "Pear"
  18. each assigned (an arbitary) int.  Basically to bahave much as an enum normally
  19. does.
  20.  
  21. In order to do this I decleared a template like:
  22.  
  23. template <char **l>
  24. class SmartEnum
  25. {
  26. public:
  27.     SmartEnum();
  28.     SmartEnum(char *);
  29.     SmartEnum(int);
  30.     //etc
  31. private:
  32.     int val;
  33.     char **names;
  34. }
  35.  
  36. template <char **nm> SmartEnum<nm>::SmartEnum()
  37. {
  38.     //what ever
  39. }
  40.  
  41. The idea being that I could declare a class based on this template which
  42. I could have multiple instances of:
  43.  
  44.     char *somenames[] = {"Apple","Banana","Pear"};
  45.     char *morenames[] = {"Peter","John","Andrew"};
  46.  
  47.     typedef SmartEnum<somenames> FruitEnum;
  48.     typedef SmartEnum<morenames> BoysEnum;
  49.     FruitEnum aFruit();
  50.     FruitEnum anotherFruit();
  51.     BoysEnum aBoy();
  52.     BoysEnum anotherBoy();
  53.  
  54. Then I could do things like:
  55.     aFruit = "Banana";
  56.     anotherFruit = 1;
  57.     aBoy = "Andrew";
  58.     anotherBoy = 1;
  59.  
  60. And (while I haven't declared these in my example because I'm too lazy) you
  61. could use them like:
  62.     cout << (int)aBoy; //gives "3"
  63.     cout << (char *)anotherBoy; //gives "Peter"
  64.     etc etc
  65.  
  66. Am I going about this the right way? Have I totally missed something?
  67. Is there A Better Way?  Should I be using templates?
  68.  
  69. Free comments welcome - even flames!!
  70.  
  71. Peter Westley             .-_!\        Hewlett Packard
  72. Phone: +61 3 (T)272 2440        /     \        Australian Support Centre
  73. Fax: +61 3 890 0326          \_.-.b/<--------31-41 Joseph St.
  74. E-mail: peterj@hparc0.aus.hp.com     v          Blackburn, Victoria 3130
  75. //
  76. // The views, opinions, information or other data here is solely from the
  77. // mind of myself.  It is not the official word from my employer.  While I
  78. // endeavour to provide accurate information, no responsibility is taken
  79. // for errors or omissions.
  80.