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

  1. Path: sparky!uunet!mcsun!sunic!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: <4858@holden.lulea.trab.se>
  6. Date: 21 Aug 92 11:56:42 GMT
  7. References: <1992Aug21.004033.15204@ucc.su.OZ.AU>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 107
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  13. : In article <4847@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  14. : I certainly dont have a more convincing argument
  15. : than the example I gave before:
  16. :     class X { 
  17. :     protected: 
  18. :         int i;
  19. :     public: 
  20. :         int get()const {return i;}
  21. :         X(int j) : i(j) {}
  22. :     };
  23. :     class Y : public virtual X {
  24. :     public: 
  25. :         Y(int j) : X(j) {}
  26. :         void set(int j) { i=j;}
  27. :     };
  28. :     f(X&);
  29. :     Y aY;
  30. :     f(Y);
  31. : I am disturbed by the fact that f can modify its X object when the
  32. : interface says it cant.
  33.  
  34. ****    I agree that this makes static typechecking more "loose".
  35.  
  36.     I think this looseness is needed, because it can be
  37.     difficult to write programs that never looses significant type
  38.     information, especially if you are using (inheriting from)
  39.     other peoples classes.
  40.  
  41.     Cast-downs are dirty (but sometimes quick) solutions
  42.     when you find you have lost significant type information.
  43.  
  44. : >    I think we agree on the need to sometimes do controlled downcasts.
  45. :     
  46. :     Dont know what you mean by 'sometimes'. I propose no
  47. : downcasting, ever.
  48.  
  49. ****    Fine, but note that, if you define X::operator Y* (),
  50.     then you are in fact suppporting controlled down-casting to Y.
  51.  
  52. :     Downcasting is an ABUSE of the concept of polymorphism,
  53. : it tries to mix up the ideas of polymorphism with heterogeneity.
  54. : These are totally different mechanisms conceptually that unfortunately
  55. : both can be implemented using inheritance.
  56.  
  57. ****    I note that the combination of polymorphism and heterogeneity
  58.     weakens the restrictions on what can be done with a polymorphic
  59.     reference.
  60.  
  61.     You have a point that a function that down-casts its arguments
  62.     is _always_ bad.
  63.  
  64.     On the other hand, down-casting a return value is not so bad:
  65.  
  66.     void g() {
  67.        Base& b = findObjectUsingCommercialLibraryThatOnlyKnowsBase();
  68.        MyDerived& d = refcast(MyDerived,b);
  69.     }
  70.  
  71.     In this case I "know" that I have previously stored an object
  72.     of type "MyDerived", but this type was not known when
  73.     the library function was written.  Templates can surely help,
  74.     but my point is that this particular use of down-casting
  75.     does not break polymorphism the way you describe it
  76.     I don't think the library function should make any assumption
  77.     about what might be done to the returned object, if it
  78.     indeed was intended to support future inheritance.
  79.  
  80.     Also, it is likely that people will run into the situation
  81.     where a template could have been used to avoid the need for
  82.     a downcast, but wasn't.
  83.  
  84. :     I see the problem is that because you can add arbitrary
  85. : derived classes polymorphically people assume they can arbitrary
  86. : classes heterogeneically too.
  87.  
  88. ****    They can, but normally shouldn't.
  89.  
  90. : >    [...]You can't find out which
  91. : >    derived classes override a virtual by inspecting the
  92. : >    base class.
  93. :     Of course not. You dont CARE how or whether the derived class
  94. : defines the virtual. Indeed you MUSTNT KNOW. And you dont NEED to know.
  95. : That lack of knowledge is power. Only by denying all knowledge
  96. : can you claim arbitrariness, and only with arbitrariness do you
  97. : have openness.
  98.  
  99. ****    These are good points.
  100.  
  101.     Which brings us to the point where I jumped into this discussion:
  102.  
  103.     Declaring explicit operators for down-casting ( X::operator Y*() )
  104.     gives the base class too much knowledge.  If you wish to
  105.     do down-casting at all, it is more "open" to do direct
  106.     down-casting, without the base class knowing about it.
  107.  
  108. -- 
  109. --------------------------------------------------------------------------
  110. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  111. | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490              |
  112. --------------------------------------------------------------------------
  113.