home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Subject: Re: Virtual base classes
- Message-ID: <rmartin.725036566@thor>
- Sender: news@rational.com
- Organization: Rational
- References: <724475492snx@trmphrst.demon.co.uk> <449@genie.UUCP>
- Date: Tue, 22 Dec 1992 15:02:46 GMT
- Lines: 55
-
- roger@genie.UUCP (Roger H. Scott) writes:
-
- |In article <724475492snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk writes:
- |>In article <445@genie.UUCP> roger@genie.UUCP (Roger H. Scott) writes:
- |>> If Kludge is a non-public subclass of WellFormed then there
- |>> isn't much point in talking about Kludge redefining WellFormed's virtual
- |>> functions since you can't use a Kludge in a place where a WellFormed is
- |>> expected (i.e., who would ever have hold of a Kludge and send it one of
- |>> WellFormed'd messages, other than a member function of Kludge?).
- |>
- |>Imagine that WellFormed's constructor adds all WellFormeds to some
- |>Collection of WellFormeds. Notice that all Kludges will also be added to
- |>this collection. Now imagine some calling some virtual function for all
- |>WellFormeds in the Collection.
-
- |This seems to raise an interesting question: does there exist an Is-A
- |relationship between Kludge and WellFormed, and if not, what does it mean to
- |have a collection of objects that *claim* to be WellFormeds but are in fact
- |Kludges?
-
- Whether there is an ISA relationship between WellFormed and Kludge
- depends upon who you are. Page 244 of the ARM says: "Members and
- friends of class X can implicitly convert an X* to a pointer to a
- private immediate base class of X." Thus, for friends and members of
- Kludge, the ISA relationship exists. But for any other function, the
- ISA relationship does not exist.
-
- Thus, a member function or friend of Kludge can convert a pointer to a
- Kludge to a pointer to a WellBuilt and return it. This will allow
- other functions to manipulate the Kludge as a WellBuilt. Its virtual
- functions will behave normally.
-
- In Nikki's example, it is the constructor of WellFormed that puts the
- newly constructed object onto a list of WellFormed. Other functions
- can then use these objects, not knowing that they are Kludges. The
- virtual functions will be deployed as usual.
-
- |Do we see a small whole in the type system here, where within
- |member functions inherited from a non-public base class an object which
- |is in fact a Kludge can be used interchangably with an object that is a
- |WellFormed (the example cited by Nikki uses the base class's constructor, but
- |any member function of the base class will do)?
-
- There is no real hole in the type system. A Kludge object can
- masquerade as a WellBuilt as long as the pointer or reference to the
- WellBuilt is created by WellBuilt's constructor, or one of the members
- or friends of Kludge. The point is that none of Kludge's clients
- knows that it is really derived from WellBuilt. This fact is
- completely hidden from them.
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-