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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!secapl!Cookie!frank
  3. From: frank@Cookie.secapl.com (Frank Adams)
  4. Subject: Re: Downcasting (was: Re: run-time type checking)
  5. Message-ID: <1992Aug22.004923.159543@Cookie.secapl.com>
  6. Date: Sat, 22 Aug 1992 00:49:23 GMT
  7. References: <9222715.29197@mulga.cs.mu.OZ.AU> <4823@holden.lulea.trab.se> <1992Aug15.170141.14667@ucc.su.OZ.AU>
  8. Organization: Security APL, Inc.
  9. Lines: 39
  10.  
  11. In article <1992Aug15.170141.14667@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  12. >
  13. >    If the function f(Base&) calls a function g(Base&)
  14. >which silently performs special actions if the reference
  15. >is to some PARTICULAR derived class, then changes to
  16. >the object can be made NOT conforming to the assumed
  17. >invariants of the base, and so f may fail to operate
  18. >correctly.
  19. >
  20. >    I will try to construct an example.
  21. >
  22. >    class X {
  23. >    protected: 
  24. >        int x;
  25. >    public:
  26. >        X(int i) : x(i) {}
  27. >        int get()const {return x;}
  28. >    };
  29. >
  30. >    f(X& a){ int n=a.get(); g(a); assert(n==a.get());}
  31. >
  32. >I would expect the assertion to obtain because class X provides
  33. >no public mechanism to modify 'x', and f has been granted and grants
  34. >to g only public access to the X object a.
  35.  
  36. Yes, in this example; but not if X has *any* virtual functions at all.  Any
  37. such function may be overridden in a subclass Y to change x, and then called
  38. by g().
  39.  
  40. >    It is sound for virtuals because they are declared!
  41. >They are there PRECISELY to allow this ability.
  42.  
  43. And the "protected" declaration is there PRECISELY to allow subclasses
  44. access to the objects so declared.  The protected declaration *delegates*
  45. preservation of assumptions regarding the protected object to the
  46. subclasses.  If you don't want that to happen, declare it private.  Why do
  47. you want to declare it protected, anyhow?
  48.  
  49.  
  50.