home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:11524 comp.std.c++:953
- Newsgroups: comp.lang.c++,comp.std.c++
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: run-time type checking (was: Re: Covariant Types in Derived Classes)
- Message-ID: <9220719.894@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <1992Jul24.143359.3602@advtech.uswest.com> <1992Jul24.234628.21196@cadsun.corp.mot.com>
- Date: Sat, 25 Jul 1992 09:04:38 GMT
- Lines: 48
-
- shang@corp.mot.com (David (Lujun) Shang) writes:
-
- >Now I give you an example that truely needs multiple dispatch.
- >
- >class Animal
- >{ Animal * fight (Animal * a)
- // simplified version for brevity
- { return this->strength() > a->strength ? this : a; }
- >};
- >
- >and we have the following fights:
- >
- >Animal * victor;
- >..
- >victor = wolf.fight (jackal);
- >victor = lynx.fight (victor);
- >victor = bull.fight (victor);
- >..
- >
- >You'll suprise me if you say, okay, I don't care who is the winner, anyway,
- >some animal or none. Then what's the purpose of the above codes? Just for
- >some thing unknown, unknown forever? You can say it is a bad design again, as
- >easily as blowing off dust. But be aware, it is an example of your beloved
- >multiple dispatch.
-
- I don't quite understand you example.
- It seems to be incomplete.
- Presumeably after all the fights, you are going to do something like
-
- if (typeof(victor) == typeof(wolf))
- cout << "The wolf won!\n";
- else if (typeof(victor) == typeof(lynx))
- cout << "The lynx won!\n";
- else ...
-
- To me that looks like horrible design.
- Why not just have
-
- cout << "The " << victor->name() << " won!\n";
-
- instead? Then there is no need for downcasting (and you also have a better
- design!).
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature VIRUS is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-