home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.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: Downcasting (was: Re: run-time type checking)
- Message-ID: <9222715.29197@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <9222518.29381@mulga.cs.mu.OZ.AU> <4820@holden.lulea.trab.se>
- Date: Fri, 14 Aug 1992 05:03:50 GMT
- Lines: 45
-
- jbn@lulea.trab.se (Johan Bengtsson) writes:
-
- >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- >:
- >:There are other ways than tagged pointers to make downcasting
- >:sound. One might be to declare the derived classes that you want
- >:to downcast to in the base class FROM which you want to downcast:
- >: class Base { //....
- virtual operator Derived1*() { return 0; }
- virtual operator Derived2*() { return 0; }
- >: };
- >:Now what does this do? Well, it preserved th open/closed principle
- >:and encapsulation and the explicit interfaces principle.
- >
- >What if someone using your classes decides to add a Derived3 class
- >to which downcasting should be possible? He must modifiy your
- >class Base, for which he may not have source code.
-
- Yes, but that would be a change to the interface!
-
- The whole point is that the ability to downcast a Base to a derived class
- SHOULD be part of the interface. If you want to change the interface, then
- of course you are going to need to modify the source code - this does not
- violate the open/closed principle.
-
- Also lattanzi@wsl.dec.com (Len Lattanzi) writes:
-
- >Biggest drawback is that Base must know about every derived class and
- >provide a virtual type conversion. But it is a usable poor-man's RTTI.
-
- Again, this is not a drawback but rather a deliberate design decision.
-
- If I pass you a Base*, I expect that you will only be able to modify it
- using the Base interface. Thus if you want to be able to modify it using
- the Derived interface, then the conversion from Base* to Derived* should
- be an explicit part of the Base interface.
-
- The real drawback is the amount of typing required. This can be reduced
- with macros if desired.
-
- --
- 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!
-