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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!CSD-NewsHost.Stanford.EDU!Xenon.Stanford.EDU!ralph
  3. From: ralph@Xenon.Stanford.EDU (Ralph Melton)
  4. Subject: 'virtual' static members
  5. Message-ID: <1992Aug12.201004.1257@CSD-NewsHost.Stanford.EDU>
  6. Originator: ralph@Xenon.Stanford.EDU
  7. Sender: news@CSD-NewsHost.Stanford.EDU
  8. Organization: Computer Science Department, Stanford University.
  9. Date: Wed, 12 Aug 1992 20:10:04 GMT
  10. Lines: 39
  11.  
  12.  
  13. I am trying to write a general enumeration class that can do output
  14. of itself.  If I was doing it as a single class, part of it might
  15. look like
  16.  
  17. class myEnum
  18. {
  19.   int value;
  20.   static int names;
  21. friend ostream& operator<< (ostream&, myEnum&);
  22. //...
  23. }
  24.  
  25. ostream& operator<< (ostream& os, myEnum& e);
  26. { return ( os << e.names[e.value] ); }
  27.  
  28.  
  29. This is all well and good, but I would like to make this into an
  30. abstract base class, so that for a specific type of MyEnum, you subclass
  31. myEnum, and give the subclass its own static "names" array.
  32. I would like to leave the operator<< function the same, if possible.
  33.  
  34. Ideally, I would like to be able to define a fully functional
  35. subclass of myEnum in just one or two lines.
  36.  
  37. Can this be done?  If I don't declare the array in myEnum, this
  38. operator<< function can't see it; if I do declare it, I can't
  39. override it in subclasses.
  40.  
  41. I think I can see more or less skanky ways around this problem using
  42. macros or specialized constructors; I am curious as to whether there
  43. is any way to declare a static member that is different for every
  44. subclass.
  45.  
  46.  
  47. Ralph Melton
  48. -- 
  49. Ralph Melton            ralph@cs.stanford.edu
  50. "Then ye shall know the truth, and the truth shall make you free."
  51.