home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: Downcasting (was: Re: run-time type checking)
- Message-ID: <4823@holden.lulea.trab.se>
- Date: 14 Aug 92 10:32:18 GMT
- References: <9222715.29197@mulga.cs.mu.OZ.AU>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 58
- X-Newsreader: Tin 1.1 PL4
-
- fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
- : jbn@lulea.trab.se (Johan Bengtsson) writes:
- :
- : >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- : >:
- : >: 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!
-
- I get your point. I also agree that the above technique
- is best (most efficient) when you have a limited set
- of known derived classes.
-
- :
- : 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.
-
- I agree that unrestricted typecasting provides the ability to
- cleanly extend the interface of a base class, into the interface
- of any derived class. Although I admit that in a way this does
- break the "closedness" of the Base class interface, I don't think
- it does so in a "bad" way.
-
- After all, inheritance allows us to break the "closedness"
- of a base class' behaviour, by overriding virtual functions.
-
- The importance is that in neither case do you have to
- intervene with the source or compiled code of the base
- class to do what you want. This is considered good
- for behaviour (overriding virtuals), so why not also
- for interfaces (unrestricted but safe downcasting)?
-
- To conclude:
-
- A base class intended for future inheritance (even by
- other programmers) should support overriding behaviour
- using virtual member functions, and should support
- interface extension by runtime-checked, safe downcasting.
-
- This lessens the risk that future users without access to the
- source code will get stuck when deriving from the base class.
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-