home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:11595 comp.std.c++:961
- Newsgroups: comp.lang.c++,comp.std.c++
- Path: sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!netmbx!jrobie
- From: jrobie@netmbx.netmbx.de (Jonathan Robie)
- Subject: Downcasting and mortal sin (was run-time type checking)
- Organization: netmbx, Berlin, Germany
- Date: Mon, 27 Jul 1992 08:50:51 GMT
- Message-ID: <RNH50ML@netmbx.netmbx.de>
- 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>
- Lines: 55
-
- glw@io.uswest.com (Glenn Williams) writes:
-
- >>(John MAX Skaller) writes:
-
- >>class Animal {
- >> int mate(Animal&);
- >>};
- >>class Dog : Animal {
- >> int mate (Animal&);
- >>};
-
- >>We REALLY want Dog::mate(Dog&), but we can't have it.
-
- I'm confused. I have two animals, and I place them in a cage.
- Who knows where these animals come from or what kind they are--
- my classes may not have produced them.
-
- Now the program needs to decide if these animals should mate.
- I assume that the program will follow the general biological rule
- and restrict mating activity to animals of the same type.
-
- I think that we need virtual functions to do this right. Otherwise
- a dog accessed with an Animal pointer will mate any other animal,
- but you can't even ask the dog if he wants to mate with anything
- other than a dog if you access it with the Dog pointer.
-
- I would think that the program should let the dog decide if this
- other animal is the kind he likes. The mating function might also
- be virtual since different animals have different habits--you can't
- expect dogs to practice safe sex, for instance. So the mate() function
- for a dog must be Dog::mate().
-
- This seems to need a down cast.
-
- So does any procedure that would attempt to determine if one animal
- is the same kind as another.
-
-
-
- Jonathan
-
- ===========================================================================
-
- Jonathan Robie jrobie@netmbx.UUCP
- Arnold-Zweig-Str. 44 jrobie@netmbx.in-berlin.de
- O-1100 Berlin
- Deutschland Phone: +37 (2) 472 04 19 (Home, East Berlin)
- +49 (30) 342 30 66 (Work, West Berlin)
-
-
- --
- Jonathan
-
- ===========================================================================
-
-