home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
- Distribution: world
- Subject: Re: run-time type checking)
- References: <4820@holden.lulea.trab.se>
- X-Mailer: cppnews $Revision: 1.14 $
- Organization: Trumphurst Ltd.
- Lines: 61
- Date: Fri, 14 Aug 1992 13:25:17 +0000
- Message-ID: <713823917snx@trmphrst.demon.co.uk>
- Sender: usenet@gate.demon.co.uk
-
-
- In article <4820@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
-
- > maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- > :
- > :There are other ways than tagged pointers to make downcasting
- > :sound. One might be to declare the derived classes that you want
- > :to downcast to in the base class FROM which you want to downcast:
- > : class Base { //....
- > : operator Derived1*() =0;
- > : operator Derived2*() =0;
- > : };
- > :Now what does this do? Well, it preserved th open/closed principle
- > :and encapsulation and the explicit interfaces principle.
- >
- > What if someone using your classes decides to add a Derived3 class
- > to which downcasting should be possible? He must modifiy your
- > class Base, for which he may not have source code.
- >
- > Does not (help) preserve the open/closed principle at all, IMHO.
- Perhaps I misunderstand the meaning of the "open/closed principle", but I
- understood that it implies that every operation which can be applied to an
- object through a reference or pointer to a Base class should be visible in
- the Base class declaration. An excellent example was presented earlier in
- this thread (but I have expired it, so I'll have to make up a new one).
-
- class Base {
- protected:
- int i;
- public:
- int get() const { return i; }
- };
-
- class Derived {
- public:
- void set(int j) { i = j; }
- };
-
- Now, if downcasting is not allowed, we can guarantee that any operation on
- a Base pointer or reference will not modify i. I understood this to be one
- of the requirements of the open/closed principle.
-
- As soon as you allow downcasting to class Derived, i may be modified. This
- violates (what I understand to be) the principle.
-
- Of course, if you state clearly in the declaration of Base that it may be
- converted into a Derived, you are also stating that i may be modified
- through a Base pointer/reference.
-
- The original poster suggested that some new syntax be invented to state
- this intention. Max's post explained (albeit with a slightly incorrect
- example :-) that no new syntax was needed, just a few (virtual) functions
- in the Base class.
-
- Corrections of any incorrect understanding on my part welcome.
- ---
- Nikki Locke | | nikki@trmphrst.demon.co.uk
- Trumphurst Ltd. | Tel: +44 (0)691-670318 | nikki@cix.compulink.co.uk
- PC and Unix consultancy | Fax: +44 (0)691-670316 | nikki@kewill.co.uk
- trmphrst.demon.co.uk is NOT connected with ANY other sites at demon.co.uk.
- Demon.co.uk is a dial-up subscription access point to the Internet.
-