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

  1. Path: sparky!uunet!airgun!airgun.wg.waii.com!bab
  2. From: bab@se39.wg2.waii.com (Brian Button)
  3. Newsgroups: comp.lang.c++
  4. Subject: Discriminating Variant Records Without switch() ??
  5. Message-ID: <BAB.92Nov10073324@se39.wg2.waii.com>
  6. Date: 10 Nov 92 12:33:24 GMT
  7. Sender: news@airgun.wg.waii.com
  8. Organization: Western Geophysical Exploration Products
  9. Lines: 44
  10. Nntp-Posting-Host: se39.wg2.waii.com
  11.  
  12. Is there a simple, OOP based way to discriminate between variant
  13. records? I saw an article in C Users Journal last month titled "Switch
  14. Deemed Harmful to C++" or something like that, but I couldn't figure
  15. out how to do it any other way.
  16.  
  17. As an example:
  18.  
  19. We're reading structures sent to us over a socket. Since these
  20. structures are being sent and received on the same machine, we can
  21. just memcpy the byte stream read from the socket into a big union, and
  22. then switch based on the first field of the union to determine the type.
  23.  
  24. As a trivial example:
  25.  
  26. {
  27.     read( sd, buf, 16 );
  28.     memcpy(( char * )&union_of_structs, buf, 16 );
  29.     switch( union_of_structs.type )
  30.     {
  31.         case A:
  32.  
  33.             .
  34.             .
  35.             .
  36.     }
  37. }
  38.  
  39. This works, but doesn't look very OOP-ish. Can anyone give me any
  40. suggestions about how this can be reformed to fit in with the object
  41. paradigm and not use the switch statement?
  42.  
  43. Thanks in advance,
  44.  
  45. bab
  46.  
  47.     
  48. --
  49. |-----------------------|----------------------------------------------------|
  50. | Brian Button          | email : button@wg2.waii.com                        |
  51. | Design Engineer       |         71023.276@compuserve.com                   |
  52. | Western Geophysical   | voice : (713)964-6221                              |
  53. | 3600 Briarpark        |----------------------------------------------------|
  54. | Houston, Texas  77042 |                  Opinions Be Mine!!                |
  55. |-----------------------|----------------------------------------------------|
  56.