home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12383 < prev    next >
Encoding:
Text File  |  1992-08-15  |  5.8 KB  |  138 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: run-time type checking)
  5. Message-ID: <1992Aug15.182304.17396@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: <4820@holden.lulea.trab.se> <713823917snx@trmphrst.demon.co.uk>
  10. Date: Sat, 15 Aug 1992 18:23:04 GMT
  11. Lines: 125
  12.  
  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 modifiy 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.  
  39.     Actually, Meyer calls that the Explicit Interfaces Principle.
  40. A module is closed if it is available for use (finished, compiled,
  41. stored in a library). A module is open if it can be extended
  42. (new fields added, new functions defined, old functions restricted)
  43. The open/closed principle states a module should be both open and
  44. closed. The mechanism of inheritance and virtual functions, that is,
  45. having classes as modules, satifies these criteria. And as we know,
  46. these are what give us polymorphism, in other words, the open/closed
  47. principle is what gives us polymorphism.
  48.  
  49.  
  50.     Note it is possible to close the interface to a class
  51. separately from the class itself. (others can compile but not
  52. yet link the class).
  53.  
  54. >An excellent example was presented earlier in 
  55. >this thread (but I have expired it, so I'll have to make up a new one).
  56. >
  57. >class Base {
  58. >protected:
  59. >    int i;
  60. >public:
  61. >    int get() const { return i; }
  62. >    };
  63. >
  64. >class Derived {
  65. >public:
  66. >    void set(int j) { i = j; }
  67. >    };
  68. >
  69. >Now, if downcasting is not allowed, we can guarantee that any operation on
  70. >a Base pointer or reference will not modify i. I understood this to be one
  71. >of the requirements of the open/closed principle.
  72.  
  73.     Actually, whats broken directly seems to be Explicit interfaces
  74. principle, and also the Information hiding principle--encapsulation
  75. is violated. What violated indirectly is the openness of the base
  76. class to arbitrary extensions, because Derived is a *special*
  77. extension in that downcasting to it is allowed. So the Base class
  78. isn't really open, because it isn't open to arbitrary extension.
  79.  
  80.     As you know from mathematics, to prove something for
  81. ALL cases it suffices to prove it for a single ARBITRARY case.
  82. ARBITRARY means in particular not relying on any special
  83. properties of that case: it means doing it for the abstract case.
  84.  
  85.     Thus in a sense, arbitrary means all means unbounded.
  86. No limits or restrictions. And the lack of restriction
  87. on what you can derive is obtained precisely from the
  88. restriction that you must stick to the abstract interface
  89. of the base. (That means the semantics cant be changed
  90. in other ways either, ways the compiler could never enforce).
  91.  
  92.     As soon as you can downcast arbitrarily, without
  93. constraint, then EVERY derived class can be made special.
  94. Where is polymorphism then? It is gone.
  95. >
  96. >As soon as you allow downcasting to class Derived, i may be modified. This
  97. >violates (what I understand to be) the principle.
  98. >
  99. >Of course, if you state clearly in the declaration of Base that it may be 
  100. >converted into a Derived, you are also stating that i may be modified 
  101. >through a Base pointer/reference.
  102.  
  103.     By making the ability to downcast Explicit in the interface
  104. of the base class, the closure of the base now depends on the listed
  105. derived classes too, so it is then open (excepting the named derived classes).
  106.  
  107. >
  108. >The original poster suggested that some new syntax be invented to state 
  109. >this intention. Max's post explained (albeit with a slightly incorrect 
  110. >example :-) that no new syntax was needed, just a few (virtual) functions 
  111. >in the Base class.
  112.  
  113.     No, probably new syntax would be used. The point was to demonstrate
  114. that using existing technology you can downcast already but with the
  115. restriction you have to declare the classes you're downcasting TO.
  116. And that that is sound (because it uses existing sound type safe nice
  117. C++ technology). I claim this restriction is required in some form
  118. to preserve open/closed principle, that is, to preserve polymorphism.
  119.  
  120. >
  121. >Corrections of any incorrect understanding on my part welcome.
  122.  
  123.  
  124.     Another method is to use tagged pointers. ALL these methods
  125. suffer from lack of extensibility that downcasting doesn't. Thats
  126. because there is only ONE method of extensibility that is sound,
  127. and we already have it. Polymorphism. Virtual functions.
  128.  
  129.     BTW: I could easily be wrong. But I'm just not convinced.
  130. The example code above shows the issue clearer than 1000 lines
  131. of argument :-)
  132.  
  133. -- 
  134. ;----------------------------------------------------------------------
  135.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  136.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  137. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  138.