home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:11608 comp.std.c++:963
- Newsgroups: comp.lang.c++,comp.std.c++
- Path: sparky!uunet!ftpbox!motsrd!news
- From: shang@corp.mot.com (David (Lujun) Shang)
- Subject: Re: run-time type checking (was: Re: Covariant Types in Derived Classes)
- Message-ID: <1992Jul27.145018.7599@cadsun.corp.mot.com>
- Sender: news@cadsun.corp.mot.com
- Reply-To: shang@corp.mot.com
- Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008
- References: <1992Jul25.172045.24675@ucc.su.OZ.AU>
- Date: Mon, 27 Jul 92 14:50:18 GMT
- Lines: 88
-
- In article <1992Jul25.172045.24675@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU
- (John MAX Skaller) writes:
- >
- > OK. My design is flawed. Why? Because I didnt write the
- > spec properly.
- >
- > Spec1: All animals can have a go at mating.
- > If they are of one species (and opposite sex) then
- > the female is flagged as pregnant.
- >
- Your spec. is still wrong. The proper spec. should be:
-
- Spec.: All animals of the same species can have a go at mating.
-
- > This spec REQUIRES the interface:
- >
- > void Animal::mate(Animal&);
- >
-
- This spec REQUIRES the interface:
-
- void Animal::mate(thiscalss&);
-
- > because the spec says 'All animals can have a go at mating'.
- >
- because the spec should prohibit possible mismatch as much as possible.
-
- > To check if the animals are of the same type, I provide
- > type information:
- >
- > virtual char* Animal::Species()=0;
- >
- Here you provide your own RTTI! We can always do that when we need RTTI.
-
- > and the sex:
- >
- > enum {male,female,infertile} sex;
- >
- > Now I can write:
- >
- > void Animal::mate(Animal& x)
- > {
- > if(sex==infertile || x.sex==infertile)return;
- > if(strcmp(Species(),x.Species())) return;
- > if(sex==x.sex)return;
- > if(sex==female) pregnant=TRUE;
- > else x.mate(*this);
- > }
- >
- If we can provide a presice specification, why should we leave so many troubles
- to the implementation?! If the implementor happened to forget to check the
- species, what is the result?! And in most cases, we can check the species at
- compile-time, why should we leave it to run-time?
-
- What I asked above just illustrates the difference of your own RTTI and the
- language supported RTTI. The latter is much better and safer.
-
- > NOW: I want to demonstrate why using RTTI is WRONG in this case,
- > and why the Species() function is required.
- >
- Remenber, your function Species() IS JUST FOR THE PURPOSE TO PROVIDE RTTI.
- >
- > The demonstration is so simple, and applies so generally, I wonder
- > why I didnt think of it before.
- >
- > class DOG : Animal { ....
- > class HUND : Animal { .. // german programmer
- >
- > See: BOTH classes are IMPLEMENTATIONS of the very same animal
- > species. But their exact types are different.
- > So they could never mate. Tell that to German Shepard
- > and an Alsatian :-)
- >
- This is never an execuse. If it is, your Species() function has the same
- problem:
-
- char* DOG::Species()
- { return "DOG"; }
- char* DOG::Species()
- { return "HOUND"; } // german programmer
-
- I'm rather confused: first, you provide your own RTTI function for this case,
- then you try to prove that using RTTI is wrong. What on earth are your trying
- to say?
-
- David Shang
-
-
-