home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11524 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  2.0 KB

  1. Xref: sparky comp.lang.c++:11524 comp.std.c++:953
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  4. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  5. Subject: Re: run-time type checking (was: Re: Covariant Types in Derived Classes)
  6. Message-ID: <9220719.894@mulga.cs.mu.OZ.AU>
  7. Sender: news@cs.mu.OZ.AU
  8. Organization: Computer Science, University of Melbourne, Australia
  9. References: <1992Jul24.143359.3602@advtech.uswest.com> <1992Jul24.234628.21196@cadsun.corp.mot.com>
  10. Date: Sat, 25 Jul 1992 09:04:38 GMT
  11. Lines: 48
  12.  
  13. shang@corp.mot.com (David (Lujun) Shang) writes:
  14.  
  15. >Now I give you an example that truely needs multiple dispatch.
  16. >
  17. >class Animal
  18. >{  Animal * fight (Animal * a)
  19.     // simplified version for brevity
  20.     {  return this->strength() > a->strength ? this : a; }
  21. >};
  22. >
  23. >and we have the following fights:
  24. >
  25. >Animal * victor;
  26. >..
  27. >victor = wolf.fight (jackal);
  28. >victor = lynx.fight (victor);
  29. >victor = bull.fight (victor);
  30. >..
  31. >
  32. >You'll suprise me if you say, okay, I don't care who is the winner, anyway,  
  33. >some animal or none. Then what's the purpose of the above codes? Just for
  34. >some thing unknown, unknown forever?  You can say it is a bad design again, as
  35. >easily as blowing off dust. But be aware, it is an example of your beloved  
  36. >multiple dispatch.
  37.  
  38. I don't quite understand you example.
  39. It seems to be incomplete.
  40. Presumeably after all the fights, you are going to do something like
  41.  
  42.     if (typeof(victor) == typeof(wolf))
  43.         cout << "The wolf won!\n";
  44.     else if (typeof(victor) == typeof(lynx))
  45.         cout << "The lynx won!\n";
  46.     else ...
  47.  
  48. To me that looks like horrible design.
  49. Why not just have
  50.  
  51.     cout << "The " << victor->name() << " won!\n"; 
  52.  
  53. instead? Then there is no need for downcasting (and you also have a better
  54. design!).
  55.  
  56. -- 
  57. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  58. This .signature VIRUS is a self-referential statement that is true - but 
  59. you will only be able to consistently believe it if you copy it to your own
  60. .signature file!
  61.