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

  1. Path: sparky!uunet!gatech!bloom-beacon!eru.mt.luth.se!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Expanding enumerators in inheriting classes
  5. Message-ID: <4818@holden.lulea.trab.se>
  6. Date: 13 Aug 92 09:49:56 GMT
  7. References: <1992Aug12.192200.6873@ntmtv>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 39
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. hildum@ntmtv.UUCP (Eric Hildum) writes:
  13. : In article <1992Aug11.190816.8623@inmet.camb.inmet.com>, stt@spock.camb.inmet.com (Tucker Taft) writes:
  14. : |> 
  15. : |> One reason we lost interest was the observation that record 
  16. : |> extension/derivation tends to make enumeration types less necessary.  
  17. : |> Rather than having a variant record/union type with a value of an enumeration
  18. : |> type distinguishing the variants, one would simply define distinct
  19. : |> derivations.  A virtual function could return a string name
  20. : |> for each distinct derivation, corresponding to the enumeration literal.
  21. : |> A separate virtual function could return an integer code, if needed.
  22. : |> 
  23. : That is run time type information via string compares, or via integer compares.
  24. : The problem with this is that now the programmer has to be concerned with the
  25. : exact values returned from each different class to prevent conflicts within
  26. : inheritance trees (and across such trees), [...]
  27.  
  28. A better approach is to have each class return a reference to a unique
  29. object, one for each class, like this:
  30.  
  31. virtual const Type& someClass :: myType();
  32. {
  33.     static Type theType;    // unique type object
  34.     return theType;
  35. }
  36.  
  37. Since objects are guaranteed to have distinct addresses,
  38. conflicts between class type identifiers are avoided.  Also,
  39. the Type class can be extended with additional operations and
  40. data.  You would probably want easy equality tests:
  41.  
  42. int operator == ( const Type& a, const Type& b )
  43. { return &a == &b; }
  44. -- 
  45. --------------------------------------------------------------------------
  46. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  47. | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490              |
  48. --------------------------------------------------------------------------
  49.