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

  1. Xref: sparky comp.lang.c++:11595 comp.std.c++:961
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!netmbx!jrobie
  4. From: jrobie@netmbx.netmbx.de (Jonathan Robie)
  5. Subject: Downcasting and mortal sin (was run-time type checking)
  6. Organization: netmbx, Berlin, Germany
  7. Date: Mon, 27 Jul 1992 08:50:51 GMT
  8. Message-ID: <RNH50ML@netmbx.netmbx.de>
  9. References: <1992Jul21.162659.25474@ucc.su.OZ.AU> <1992Jul22.022218.1115@cadsun.corp.mot.com> <1992Jul23.154254.5306@ucc.su.OZ.AU> <1992Jul24.143359.3602@advtech.uswest.com>
  10. Lines: 55
  11.  
  12. glw@io.uswest.com (Glenn Williams) writes:
  13.  
  14. >>(John MAX Skaller) writes:
  15.  
  16. >>class Animal {
  17. >>    int mate(Animal&);
  18. >>};
  19. >>class Dog : Animal {
  20. >>    int mate (Animal&);
  21. >>};
  22.  
  23. >>We REALLY want Dog::mate(Dog&), but we can't have it.
  24.  
  25. I'm confused.  I have two animals, and I place them in a cage.
  26. Who knows where these animals come from or what kind they are--
  27. my classes may not have produced them.
  28.  
  29. Now the program needs to decide if these animals should mate.
  30. I assume that the program will follow the general biological rule
  31. and restrict mating activity to animals of the same type.
  32.  
  33. I think that we need virtual functions to do this right.  Otherwise
  34. a dog accessed with an Animal pointer will mate any other animal,
  35. but you can't even ask the dog if he wants to mate with anything
  36. other than a dog if you access it with the Dog pointer.
  37.  
  38. I would think that the program should let the dog decide if this
  39. other animal is the kind he likes.  The mating function might also
  40. be virtual since different animals have different habits--you can't
  41. expect dogs to practice safe sex, for instance.  So the mate() function
  42. for a dog must be Dog::mate().
  43.  
  44. This seems to need a down cast.
  45.  
  46. So does any procedure that would attempt to determine if one animal
  47. is the same kind as another.
  48.  
  49.  
  50.  
  51. Jonathan
  52.  
  53. ===========================================================================
  54.  
  55. Jonathan Robie        jrobie@netmbx.UUCP
  56. Arnold-Zweig-Str. 44    jrobie@netmbx.in-berlin.de
  57. O-1100 Berlin        
  58. Deutschland        Phone:  +37 (2) 472 04 19  (Home, East Berlin)
  59.                 +49 (30) 342 30 66 (Work, West Berlin)
  60.  
  61.  
  62. -- 
  63. Jonathan
  64.  
  65. ===========================================================================
  66.  
  67.