home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12324 < prev    next >
Encoding:
Text File  |  1992-08-13  |  2.9 KB  |  76 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!newsgate.watson.ibm.com!news.ans.net!cmcl2!HARRISON.CS.NYU.EDU!osinski
  3. From: osinski@HARRISON.CS.NYU.EDU (Ed Osinski)
  4. Subject: Re: 'virtual' static members
  5. Message-ID: <9208132115.AA24368@HARRISON.CS.NYU.EDU>
  6. Sender: daemon@cmcl2.nyu.edu (Mr Background)
  7. Reply-To: osinski@cs.nyu.edu
  8. Organization: New York University
  9. References: <1992Aug12.201004.1257@CSD-NewsHost.Stanford.EDU>
  10. Date: Thu, 13 Aug 1992 21:15:47 GMT
  11. Lines: 63
  12.  
  13. In article <1992Aug12.201004.1257@CSD-NewsHost.Stanford.EDU>, ralph@Xenon.Stanford.EDU (Ralph Melton) writes:
  14. |> 
  15. |> I am trying to write a general enumeration class that can do output
  16. |> of itself.  If I was doing it as a single class, part of it might
  17. |> look like
  18. |> 
  19. |> class myEnum
  20. |> {
  21. |>   int value;
  22. |>   static int names;
  23. |> friend ostream& operator<< (ostream&, myEnum&);
  24. |> //...
  25. |> }
  26. |> 
  27. |> ostream& operator<< (ostream& os, myEnum& e);
  28. |> { return ( os << e.names[e.value] ); }
  29. |> 
  30. |> 
  31. |> This is all well and good, but I would like to make this into an
  32. |> abstract base class, so that for a specific type of MyEnum, you subclass
  33. |> myEnum, and give the subclass its own static "names" array.
  34. |> I would like to leave the operator<< function the same, if possible.
  35. |> 
  36. |> Ideally, I would like to be able to define a fully functional
  37. |> subclass of myEnum in just one or two lines.
  38. |> 
  39. |> Can this be done?  If I don't declare the array in myEnum, this
  40. |> operator<< function can't see it; if I do declare it, I can't
  41. |> override it in subclasses.
  42. |> 
  43. |> I think I can see more or less skanky ways around this problem using
  44. |> macros or specialized constructors; I am curious as to whether there
  45. |> is any way to declare a static member that is different for every
  46. |> subclass.
  47.  
  48. No there isn't, but you can get the same effect by declaring static data
  49. members in each class to hold the table, and defining a virtual member function
  50. that returns that class's table.  In your print function, instead of referring
  51. to "names", you would refer to "enum_obj.get_names()".  This will require each
  52. class to have:
  53.  
  54.     - a new static data member for the table (the variable name can be
  55.           the same in each class)
  56.         - an initialization of that static data member (which must be done
  57.       outside the class)
  58.     - a redefinition of the "get_names" function which simply returns the
  59.           static data member specifying the table of that class
  60.  
  61. |> 
  62. |> 
  63. |> Ralph Melton
  64. |> -- 
  65. |> Ralph Melton            ralph@cs.stanford.edu
  66. |> "Then ye shall know the truth, and the truth shall make you free."
  67.  
  68. -----------------------------------------------------------------------
  69.  Ed Osinski                  |
  70.  Computer Science Department |
  71.  New York University         |           This space for rent
  72.  E-mail:  osinski@cs.nyu.edu |
  73.  Voice:   (212) 998-3515     |
  74.  Fax:     (212) 995-4123     |
  75. -----------------------------------------------------------------------
  76.