home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12526 < prev    next >
Encoding:
Internet Message Format  |  1992-08-18  |  3.3 KB

  1. Path: sparky!uunet!olivea!hal.com!decwrl!deccrl!bloom-beacon!eru.mt.luth.se!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Downcasting (was: Re: run-time type checking)
  5. Message-ID: <4847@holden.lulea.trab.se>
  6. Date: 18 Aug 92 18:02:01 GMT
  7. References: <1992Aug15.170141.14667@ucc.su.OZ.AU>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 66
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  13. : In article <4823@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  14. : >: >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  15. : >: >:
  16. : >: >:    class Base { //....
  17. : >:         virtual operator Derived1*() { return 0; }
  18. : >:         virtual operator Derived2*() { return 0; }
  19. : >: >:    };
  20. [...]
  21. : >    I agree that unrestricted typecasting provides the ability to
  22. : >    cleanly extend the interface of a base class, into the interface
  23. : >    of any derived class.  Although I admit that in a way this does
  24. : >    break the "closedness" of the Base class interface, I don't think
  25. : >    it does so in a "bad" way.
  26. :     This is the issue. You might be right, but I'm not convinced.
  27. : In a statically secure system, I argue that it is important to access
  28. : an object only by its explicitly declared accessible interface.
  29.  
  30. ****    I think _that_ is the issue.  I'd say a system can be statically
  31.     secure, even if it does (undeclared) runtime-checked down-casts
  32.     (that is, if the program only does the down-casts when possible).
  33.     I think we agree on the need to sometimes do controlled downcasts.
  34.  
  35.     I also admit that when you see a need for a downcast, you should
  36.     ask yourself why you have lost track of the type of the object,
  37.     and change the code to avoid it, if possible.  Downcasting is a
  38.     last resort.  Yes, I use GOTOs too (in the form of break/continue
  39.     statements).
  40.  
  41.     You may argue that the system is easier to _maintain_ by making the
  42.     possible down-casts visible in the base class, and I would agree.
  43.  
  44. : >
  45. : >    The importance is that in neither case do you have to
  46. : >    intervene with the source or compiled code of the base
  47. : >    class to do what you want.  This is considered good
  48. : >    for behaviour (overriding virtuals), so why not also
  49. : >    for interfaces (unrestricted but safe downcasting)?
  50. :     It is sound for virtuals because they are declared!
  51.  
  52. ****    But not in the base class!  You can't find out which
  53.     derived classes override a virtual by inspecting the
  54.     base class.
  55.  
  56. : >    A base class intended for future inheritance (even by
  57. : >    other programmers) should support overriding behaviour
  58. : >    using virtual member functions, and should support
  59. : >    interface extension by runtime-checked, safe downcasting.
  60. : >
  61. : >    This lessens the risk that future users without access to the
  62. : >    source code will get stuck when deriving from the base class.
  63. : The point of polymorphism is that
  64. : your should not rely on derived class behaviour in routines
  65. : given a base object, and you shouldn't need to.
  66.  
  67. ****    But you do, since any derived class may have an
  68.     incorrect implementation of a virtual method.
  69.  
  70. -- 
  71. --------------------------------------------------------------------------
  72. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  73. | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490              |
  74. --------------------------------------------------------------------------
  75.