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: .. ptr_cast ..
- Message-ID: <rmartin.725047369@thor>
- Keywords: ptr_cast
- Sender: news@rational.com
- Organization: Rational
- References: <1992Dec21.112355.6061@fwi.uva.nl>
- Date: Tue, 22 Dec 1992 18:02:49 GMT
- Lines: 64
-
- koelma@fwi.uva.nl (Dennis Koelma) writes:
-
- |Hi,
-
- |Does anybody know whether the function "ptr_cast", page 442 in
- |"The C++ programming language" by Stroustrup, is or will be supported
- |by the Sun compiler ?
-
- This proposal has undergone a few changes. the ptr_cast syntax has
- been replaced with regular cast syntax. I am not certain of the
- status of this proposal with the standards committee.
-
- |If not, is there some "workaround" library available ?
-
- It is not very hard to implement your own RTTI system, although it is
- a bit tedious and prone to error. You might want to look at the
- scheme used by the NIH classes, just for educating yourself on one
- kind of method.
-
- The trick to any kind of RTTI is to be able to answer the following
- question: Is object X derived from class Y. The answer of this
- question must be TRUE if X is an instance of Y, or an instance of any
- class derived from Y. (Consider the complexities that this implies
- for MI).
-
- In general, to be able to answer this question, you must have some
- kind of ID which is unique for every different class. Then a virtual
- function which all objects posess which asks the question. When the
- function executes, it checks the ID of the object with the ID in the
- query. If they match, it returns TRUE, otherwise it returns the
- result gotten by calling the same function in each of the base
- classes. At the top of the inheritance hierarchy there is a class
- which all other classes must derive from. Call it "Object". This
- class declares the virtual query function and creates an
- implementation which returns FALSE.
-
- You can work out the rest of the details for yourself, it is not real
- hard.
-
- However, it should be noted that implementing such schemes places a
- burden upon your class hierarchy. Every class must have the ID, every
- class must implement your query function, etc, etc. Third party
- classes cannot directly participate, someone must be in charge of
- dispensing unique ID codes to prevent the possibility of overlap, etc,
- etc...
-
- If you get the idea that I don't like to see schemes like this, you
- are right. If at all possible, I would wait for the compiler vendors
- to implement the RTTI proposal.
-
- Once you have RTTI, the temptation is to use it all the time. Taken
- to its extreme, this will cause you to undo many of the benefits of
- OO. You should be very jealous of the secrecy of the type of an
- object. It should be exposed only in the most extraordinary of
- circumstances. There are certainly cases where RTTI is useful, but is
- is also very easy to abuse.
-
-
-
- --
- 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++
-