home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!secapl!Cookie!frank
- From: frank@Cookie.secapl.com (Frank Adams)
- Subject: Re: Downcasting (was: Re: run-time type checking)
- Message-ID: <1992Aug22.004923.159543@Cookie.secapl.com>
- Date: Sat, 22 Aug 1992 00:49:23 GMT
- References: <9222715.29197@mulga.cs.mu.OZ.AU> <4823@holden.lulea.trab.se> <1992Aug15.170141.14667@ucc.su.OZ.AU>
- Organization: Security APL, Inc.
- Lines: 39
-
- In article <1992Aug15.170141.14667@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- >
- > If the function f(Base&) calls a function g(Base&)
- >which silently performs special actions if the reference
- >is to some PARTICULAR derived class, then changes to
- >the object can be made NOT conforming to the assumed
- >invariants of the base, and so f may fail to operate
- >correctly.
- >
- > I will try to construct an example.
- >
- > class X {
- > protected:
- > int x;
- > public:
- > X(int i) : x(i) {}
- > int get()const {return x;}
- > };
- >
- > f(X& a){ int n=a.get(); g(a); assert(n==a.get());}
- >
- >I would expect the assertion to obtain because class X provides
- >no public mechanism to modify 'x', and f has been granted and grants
- >to g only public access to the X object a.
-
- Yes, in this example; but not if X has *any* virtual functions at all. Any
- such function may be overridden in a subclass Y to change x, and then called
- by g().
-
- > It is sound for virtuals because they are declared!
- >They are there PRECISELY to allow this ability.
-
- And the "protected" declaration is there PRECISELY to allow subclasses
- access to the objects so declared. The protected declaration *delegates*
- preservation of assumptions regarding the protected object to the
- subclasses. If you don't want that to happen, declare it private. Why do
- you want to declare it protected, anyhow?
-
-
-