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

  1. Path: sparky!uunet!olivea!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: run-time type checking)
  5. Message-ID: <4846@holden.lulea.trab.se>
  6. Date: 18 Aug 92 16:12:46 GMT
  7. References: <1992Aug15.182304.17396@ucc.su.OZ.AU>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 78
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  13. : In article <713823917snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk (Nikki Locke) writes:
  14. : >
  15. : >In article <4820@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  16. : >
  17. : >> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  18. : >> :
  19. : >> :There are other ways than tagged pointers to make downcasting
  20. : >> :sound. One might be to declare the derived classes that you want
  21. : >> :to downcast to in the base class FROM which you want to downcast:
  22. : >> :    class Base { //....
  23. : >> :        operator Derived1*() =0;
  24. : >> :        operator Derived2*() =0;
  25. : >> :    };
  26. : >> :Now what does this do? Well, it preserved th open/closed principle
  27. : >> :and encapsulation and the explicit interfaces principle.
  28. : >> 
  29. : >> What if someone using your classes decides to add a Derived3 class
  30. : >> to which downcasting should be possible?  He must modify your
  31. : >> class Base, for which he may not have source code.
  32. : >> 
  33. : >> Does not (help) preserve the open/closed principle at all, IMHO.
  34. : >Perhaps I misunderstand the meaning of the "open/closed principle", but I 
  35. : >understood that it implies that every operation which can be applied to an
  36. : >object through a reference or pointer to a Base class should be visible in 
  37. : >the Base class declaration. 
  38. :     Actually, Meyer calls that the Explicit Interfaces Principle.
  39.  
  40. ****    From Meyer (p21) [Explicit interfaces]:
  41.  
  42.     "Whenever two modules A and B communicate, this must be
  43.      obvious from the text of A or B or both".
  44.  
  45.     You take this to mean "...obvious from the interface of
  46.     A or B", which is not what the author said, but perhaps what
  47.     he meant.  Fine.
  48.     
  49. : A module is closed if it is available for use (finished, compiled,
  50. : stored in a library). A module is open if it can be extended
  51. : (new fields added, new functions defined, old functions restricted)
  52. : The open/closed principle states a module should be both open and
  53. : closed. The mechanism of inheritance and virtual functions, that is,
  54. : having classes as modules, satifies these criteria.
  55.  
  56. ****    Right.
  57.  
  58.     I would like to argue that your initial suggestion means that you
  59.     will have to go back and extend the interface of a base class,
  60.     to support operations in a new derived class.  If this is
  61.     indeed an option for you, why don't you instead add the new
  62.     operations as virtual functions of the base class?  The base
  63.     class interface was after all not really closed, was it?
  64.  
  65.     Even Eiffel has "reverse assignments" to do safe
  66.     downcasting (kind of supports interface openness).
  67.  
  68. :     By making the ability to downcast Explicit in the interface
  69. : of the base class, the closure of the base now depends on the listed
  70. : derived classes too, so it is then open (excepting the named derived classes).
  71.  
  72. ****    I agree that this is a good thing, _if you can do it_. Base
  73.     classes that will be extended by other programmers cannot
  74.     in general list all future derived classes.
  75.  
  76. :     Another method is to use tagged pointers. ALL these methods
  77. : suffer from lack of extensibility that downcasting doesn't. Thats
  78. : because there is only ONE method of extensibility that is sound,
  79. : and we already have it. Polymorphism. Virtual functions.
  80.  
  81. ****    Sound or not, when polymorphism does not suffice, you
  82.     have to rely on something else, right?
  83.  
  84. -- 
  85. --------------------------------------------------------------------------
  86. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  87. | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490              |
  88. --------------------------------------------------------------------------
  89.