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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Downcasting (was: Re: run-time type checking)
  5. Message-ID: <9222715.29197@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <9222518.29381@mulga.cs.mu.OZ.AU> <4820@holden.lulea.trab.se>
  9. Date: Fri, 14 Aug 1992 05:03:50 GMT
  10. Lines: 45
  11.  
  12. jbn@lulea.trab.se (Johan Bengtsson) writes:
  13.  
  14. >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  15. >:
  16. >:There are other ways than tagged pointers to make downcasting
  17. >:sound. One might be to declare the derived classes that you want
  18. >:to downcast to in the base class FROM which you want to downcast:
  19. >:    class Base { //....
  20.         virtual operator Derived1*() { return 0; }
  21.         virtual operator Derived2*() { return 0; }
  22. >:    };
  23. >:Now what does this do? Well, it preserved th open/closed principle
  24. >:and encapsulation and the explicit interfaces principle.
  25. >
  26. >What if someone using your classes decides to add a Derived3 class
  27. >to which downcasting should be possible?  He must modifiy your
  28. >class Base, for which he may not have source code.
  29.  
  30. Yes, but that would be a change to the interface!
  31.  
  32. The whole point is that the ability to downcast a Base to a derived class
  33. SHOULD be part of the interface. If you want to change the interface, then
  34. of course you are going to need to modify the source code - this does not
  35. violate the open/closed principle.
  36.  
  37. Also lattanzi@wsl.dec.com (Len Lattanzi) writes:
  38.  
  39. >Biggest drawback is that Base must know about every derived class and
  40. >provide a virtual type conversion. But it is a usable poor-man's RTTI.
  41.  
  42. Again, this is not a drawback but rather a deliberate design decision.
  43.  
  44. If I pass you a Base*, I expect that you will only be able to modify it
  45. using the Base interface. Thus if you want to be able to modify it using
  46. the Derived interface, then the conversion from Base* to Derived* should
  47. be an explicit part of the Base interface.
  48.  
  49. The real drawback is the amount of typing required. This can be reduced
  50. with macros if desired.
  51.  
  52. -- 
  53. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  54. This .signature VIRUS is a self-referential statement that is true - but 
  55. you will only be able to consistently believe it if you copy it to your own
  56. .signature file!
  57.