home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16095 < prev    next >
Encoding:
Internet Message Format  |  1992-11-11  |  2.4 KB

  1. Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!sol.ctr.columbia.edu!emory!swrinde!cs.utexas.edu!convex!news.utdallas.edu!corpgate!crchh327!bnr.ca!holiday
  2. From: holiday@bnr.ca (Matthew Holiday)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Discriminating Variant Records Without switch() ??
  5. Message-ID: <1992Nov11.144253.26415@bnr.ca>
  6. Date: 11 Nov 92 14:42:53 GMT
  7. References: <BAB.92Nov10073324@se39.wg2.waii.com>
  8. Sender: holiday@crchh82 (Matthew Holiday)
  9. Reply-To: holiday@bnr.ca
  10. Organization: Bell-Northern Research, Richardson, TX
  11. Lines: 38
  12. Nntp-Posting-Host: crchh82
  13.  
  14. In article <BAB.92Nov10073324@se39.wg2.waii.com>, bab@se39.wg2.waii.com (Brian Button) writes:
  15. |> Is there a simple, OOP based way to discriminate between variant
  16. |> records? I saw an article in C Users Journal last month titled "Switch
  17. |> Deemed Harmful to C++" or something like that, but I couldn't figure
  18. |> out how to do it any other way.
  19. |> 
  20. |> As an example:
  21. |> 
  22. |> We're reading structures sent to us over a socket. Since these
  23. |> structures are being sent and received on the same machine, we can
  24. |> just memcpy the byte stream read from the socket into a big union, and
  25. |> then switch based on the first field of the union to determine the type.
  26. |> 
  27. |> [text ommitted]
  28. |> 
  29. |> This works, but doesn't look very OOP-ish. Can anyone give me any
  30. |> suggestions about how this can be reformed to fit in with the object
  31. |> paradigm and not use the switch statement?
  32.  
  33. Another (not quite OOP-ish) way is to have an array of function pointers,
  34. and index the array with the first field of the union.  Note that you need
  35. an appropriate nil function for all the array entries which represent invalid
  36. indices.  This is pretty straightforward.
  37.  
  38. The OOP-ish way would be to construct an object of the appropriate class to
  39. match your input message, and then call that object's methods (which would
  40. be overridden as necessary for specific subclasses).  Unfortunately, the
  41. part of your code where you construct the object is likely to be non-OOP-ish,
  42. in fact, it'll probably require a switch statement! :-)  There ought to be a
  43. convenient way to create objects from network messages, i.e., some way to
  44. register message types and their matching class types, such that for every
  45. incoming message, its body (an array of bytes) gets turned into the correct
  46. object, which would be derived from a root generic message object.
  47.  
  48. -- 
  49. Matt Holiday                                      #include <std/disclaimer>
  50. holiday@bnr.ca
  51. BNR Richardson, TX
  52.