home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12683 < prev    next >
Encoding:
Text File  |  1992-08-20  |  6.5 KB  |  178 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Downcasting (was: Re: run-time type checking)
  5. Message-ID: <1992Aug21.004033.15204@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1992Aug15.170141.14667@ucc.su.OZ.AU> <4847@holden.lulea.trab.se>
  10. Date: Fri, 21 Aug 1992 00:40:33 GMT
  11. Lines: 165
  12.  
  13. In article <4847@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  14. >: In a statically secure system, I argue that it is important to access
  15. >: an object only by its explicitly declared accessible interface.
  16. >
  17. >****    I think _that_ is the issue.  I'd say a system can be statically
  18. >    secure, even if it does (undeclared) runtime-checked down-casts
  19. >    (that is, if the program only does the down-casts when possible).
  20.  
  21.     You may be right, I certainly dont have a more convincing argument
  22. than the example I gave before:
  23.  
  24.     class X { 
  25.     protected: 
  26.         int i;
  27.     public: 
  28.         int get()const {return i;}
  29.         X(int j) : i(j) {}
  30.     };
  31.  
  32.     class Y : public virtual X {
  33.     public: 
  34.         Y(int j) : X(j) {}
  35.         void set(int j) { i=j;}
  36.     };
  37.  
  38.     f(X&);
  39.     Y aY;
  40.     f(Y);
  41.  
  42. I am disturbed by the fact that f can modify its X object when the
  43. interface says it cant. 
  44.  
  45. >    I think we agree on the need to sometimes do controlled downcasts.
  46.     
  47.     Dont know what you mean by 'sometimes'. I propose no
  48. downcasting, ever. Ban even existing legal downcasts. I know this
  49. is overly restrictive, but any loosening of the restriction I would
  50. accept without further involved argument cannot do what the
  51. downcasting advocates want---to downcast to a class without
  52. explicit interface modification is necessary for extensibility.
  53.  
  54.     The issue then is whether there is a price to pay for the
  55. extensibility or not. I argue there is, although I could be wrong.
  56. To convince me you have to assert that nothing valuable is lost
  57. in the case given above. In that case, the assumption I could make
  58. that Y.i remains constant over the call to f is violated if
  59. downcasting is allowed. At least there is a lost optimisation
  60. opportunity. At worst all of polymorphism is destroyed.
  61. >
  62. >    I also admit that when you see a need for a downcast, you should
  63. >    ask yourself why you have lost track of the type of the object,
  64. >    and change the code to avoid it, if possible.  Downcasting is a
  65. >    last resort.  Yes, I use GOTOs too (in the form of break/continue
  66. >    statements).
  67.  
  68.     Downcasting is an ABUSE of the concept of polymorphism,
  69. it tries to mix up the ideas of polymorphism with heterogeneity.
  70. These are totally different mechanisms conceptually that unfortunately
  71. both can be implemented using inheritance.
  72.  
  73. >
  74. >    You may argue that the system is easier to _maintain_ by making the
  75. >    possible down-casts visible in the base class, and I would agree.
  76.  
  77.     No, I argue that to mix polymorphism and heteromorphism
  78. in inheritance, you need to distinguish which 'bits' are poly
  79. morphic and which are hetero-morphic.
  80.  
  81.     I see the problem is that because you can add arbitrary
  82. derived classes polymorphically people assume they can arbitrary
  83. classes heterogeneically too. This is false IMHO. Heteromorphic
  84. extensions are always finite and non-arbitrary (there is a specific
  85. number of them, and you need to handle all the cases separately,
  86. i.e., you need a compile time 'switch'.) But polymorphism 
  87. handles infinite (arbitrary) cases by using indirection,
  88. i.e. vectoring. Here the address is not known, but the address
  89. of the address is. The actual function is not known, but 
  90. the addresses of all the functions ARE known.
  91.  
  92.     A switch does not and cannot have this property.
  93. The fact that downcasting requires
  94.  
  95.     a) Run-time type info
  96.     b) Compile time explicit case codes to be written
  97.  
  98. the most dynmaic and most static of concepts togther, seems to say
  99. something is inappropriate. Mm.
  100.  
  101.     Lets have a look at this:
  102.  
  103.     void (*f)(X&); // function pointer
  104.     ...
  105.     f(x);        // vectored call
  106.  
  107. This is statically typesafe and works for ARBITRARY functions
  108. that might be assigned to f. The price one pays for the ARBITRARYness
  109. is that ALL the functions you can assign to f MUST have the same
  110. interface.
  111.  
  112.     Now:
  113.  
  114.     switch (...)
  115.     case ..:  f(1);
  116.     case ..: f('m',2.1);
  117.     ...
  118.  
  119. Here there is a fixed list of cases, and the f's are unrelated 
  120. except by name. They are quite specific f's, and take different
  121. parameters. Also type safe.
  122.  
  123. Now the downcasters want to do BOTH the above simultaneously,
  124. to have type safe arbitrary switching/vectoring, and I dont
  125. believe its possible.
  126.  
  127. >
  128. >: >
  129. >: >    The importance is that in neither case do you have to
  130. >: >    intervene with the source or compiled code of the base
  131. >: >    class to do what you want.  This is considered good
  132. >: >    for behaviour (overriding virtuals), so why not also
  133. >: >    for interfaces (unrestricted but safe downcasting)?
  134. >: 
  135. >:     It is sound for virtuals because they are declared!
  136. >
  137. >****    But not in the base class!  You can't find out which
  138. >    derived classes override a virtual by inspecting the
  139. >    base class.
  140.  
  141.     Of course not. You dont CARE how or whether the derived class
  142. defines the virtual. Indeed you MUSTNT KNOW. And you dont NEED to know.
  143. That lack of knowledge is power. Only by denying all knowledge
  144. can you claim arbitrariness, and only with arbitrariness do you
  145. have openness.
  146. >
  147. >: >    A base class intended for future inheritance (even by
  148. >: >    other programmers) should support overriding behaviour
  149. >: >    using virtual member functions, and should support
  150. >: >    interface extension by runtime-checked, safe downcasting.
  151. >: >
  152. >: >    This lessens the risk that future users without access to the
  153. >: >    source code will get stuck when deriving from the base class.
  154. >: 
  155. >: The point of polymorphism is that
  156. >: your should not rely on derived class behaviour in routines
  157. >: given a base object, and you shouldn't need to.
  158. >
  159. >****    But you do, since any derived class may have an
  160. >    incorrect implementation of a virtual method.
  161. >
  162.     Yes, but that is another issue. In principle
  163. there are invariants etc, and you shouldnt violate them
  164. even if the language can't check them.
  165.  
  166.     You MUST rely on the implementor of a derived class
  167. not violating the semantics of the base class, because there is
  168. no other option. You cant check the semantics of an arbitrary
  169. and not yet written derived class in the base class completely,
  170. although invariants might help narrow the illegal
  171. cases down.
  172.  
  173. -- 
  174. ;----------------------------------------------------------------------
  175.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  176.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  177. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  178.