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

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Distribution: world
  5. Subject: Re: run-time type checking)
  6. References: <4820@holden.lulea.trab.se>
  7. X-Mailer: cppnews $Revision: 1.14 $
  8. Organization: Trumphurst Ltd.
  9. Lines: 61
  10. Date: Fri, 14 Aug 1992 13:25:17 +0000
  11. Message-ID: <713823917snx@trmphrst.demon.co.uk>
  12. Sender: usenet@gate.demon.co.uk
  13.  
  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. > What if someone using your classes decides to add a Derived3 class
  29. > to which downcasting should be possible?  He must modifiy your
  30. > class Base, for which he may not have source code.
  31. > Does not (help) preserve the open/closed principle at all, IMHO.
  32. Perhaps I misunderstand the meaning of the "open/closed principle", but I 
  33. understood that it implies that every operation which can be applied to an
  34. object through a reference or pointer to a Base class should be visible in 
  35. the Base class declaration. An excellent example was presented earlier in 
  36. this thread (but I have expired it, so I'll have to make up a new one).
  37.  
  38. class Base {
  39. protected:
  40.     int i;
  41. public:
  42.     int get() const { return i; }
  43.     };
  44.  
  45. class Derived {
  46. public:
  47.     void set(int j) { i = j; }
  48.     };
  49.  
  50. Now, if downcasting is not allowed, we can guarantee that any operation on
  51. a Base pointer or reference will not modify i. I understood this to be one
  52. of the requirements of the open/closed principle.
  53.  
  54. As soon as you allow downcasting to class Derived, i may be modified. This
  55. violates (what I understand to be) the principle.
  56.  
  57. Of course, if you state clearly in the declaration of Base that it may be 
  58. converted into a Derived, you are also stating that i may be modified 
  59. through a Base pointer/reference.
  60.  
  61. The original poster suggested that some new syntax be invented to state 
  62. this intention. Max's post explained (albeit with a slightly incorrect 
  63. example :-) that no new syntax was needed, just a few (virtual) functions 
  64. in the Base class.
  65.  
  66. Corrections of any incorrect understanding on my part welcome.
  67. ---
  68. Nikki Locke              |                        | nikki@trmphrst.demon.co.uk
  69. Trumphurst Ltd.          | Tel: +44 (0)691-670318 | nikki@cix.compulink.co.uk
  70. PC and Unix consultancy  | Fax: +44 (0)691-670316 | nikki@kewill.co.uk
  71. trmphrst.demon.co.uk is NOT connected with ANY other sites at demon.co.uk.
  72. Demon.co.uk is a dial-up subscription access point to the Internet.
  73.