home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!hal.com!decwrl!deccrl!bloom-beacon!eru.mt.luth.se!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: <4847@holden.lulea.trab.se>
- Date: 18 Aug 92 18:02:01 GMT
- References: <1992Aug15.170141.14667@ucc.su.OZ.AU>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 66
- X-Newsreader: Tin 1.1 PL4
-
- maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- : In article <4823@holden.lulea.trab.se> 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; }
- : >: >: };
- [...]
- : > 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.
- :
- : This is the issue. You might be right, but I'm not convinced.
- : In a statically secure system, I argue that it is important to access
- : an object only by its explicitly declared accessible interface.
-
- **** I think _that_ is the issue. I'd say a system can be statically
- secure, even if it does (undeclared) runtime-checked down-casts
- (that is, if the program only does the down-casts when possible).
- I think we agree on the need to sometimes do controlled downcasts.
-
- I also admit that when you see a need for a downcast, you should
- ask yourself why you have lost track of the type of the object,
- and change the code to avoid it, if possible. Downcasting is a
- last resort. Yes, I use GOTOs too (in the form of break/continue
- statements).
-
- You may argue that the system is easier to _maintain_ by making the
- possible down-casts visible in the base class, and I would agree.
-
- : >
- : > 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)?
- :
- : It is sound for virtuals because they are declared!
-
- **** But not in the base class! You can't find out which
- derived classes override a virtual by inspecting the
- base class.
-
- : > 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.
- :
- : The point of polymorphism is that
- : your should not rely on derived class behaviour in routines
- : given a base object, and you shouldn't need to.
-
- **** But you do, since any derived class may have an
- incorrect implementation of a virtual method.
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-