home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!airgun!airgun.wg.waii.com!bab
- From: bab@se39.wg2.waii.com (Brian Button)
- Newsgroups: comp.lang.c++
- Subject: Discriminating Variant Records Without switch() ??
- Message-ID: <BAB.92Nov10073324@se39.wg2.waii.com>
- Date: 10 Nov 92 12:33:24 GMT
- Sender: news@airgun.wg.waii.com
- Organization: Western Geophysical Exploration Products
- Lines: 44
- Nntp-Posting-Host: se39.wg2.waii.com
-
- Is there a simple, OOP based way to discriminate between variant
- records? I saw an article in C Users Journal last month titled "Switch
- Deemed Harmful to C++" or something like that, but I couldn't figure
- out how to do it any other way.
-
- As an example:
-
- We're reading structures sent to us over a socket. Since these
- structures are being sent and received on the same machine, we can
- just memcpy the byte stream read from the socket into a big union, and
- then switch based on the first field of the union to determine the type.
-
- As a trivial example:
-
- {
- read( sd, buf, 16 );
- memcpy(( char * )&union_of_structs, buf, 16 );
- switch( union_of_structs.type )
- {
- case A:
-
- .
- .
- .
- }
- }
-
- This works, but doesn't look very OOP-ish. Can anyone give me any
- suggestions about how this can be reformed to fit in with the object
- paradigm and not use the switch statement?
-
- Thanks in advance,
-
- bab
-
-
- --
- |-----------------------|----------------------------------------------------|
- | Brian Button | email : button@wg2.waii.com |
- | Design Engineer | 71023.276@compuserve.com |
- | Western Geophysical | voice : (713)964-6221 |
- | 3600 Briarpark |----------------------------------------------------|
- | Houston, Texas 77042 | Opinions Be Mine!! |
- |-----------------------|----------------------------------------------------|
-