Xref: utzoo comp.object:1466 comp.lang.c++:8469 Path: utzoo!utgpu!watserv1!watmath!att!rutgers!psuvax1!uwm.edu!cs.utexas.edu!uunet!microsoft!bobatk From: bobatk@microsoft.UUCP (Bob ATKINSON) Newsgroups: comp.object,comp.lang.c++ Subject: Re: inheritance and `type loss' Message-ID: <55845@microsoft.UUCP> Date: 16 Jul 90 17:52:22 GMT References: <1130013@gore.com> Reply-To: bobatk@microsoft.UUCP (Bob ATKINSON) Organization: Microsoft Corp., Redmond WA Lines: 53 In article <1130013@gore.com> jacob@gore.com (Jacob Gore) writes: >/ comp.object / sra@ecs.soton.ac.uk (Stephen Adams) / Jul 10, 1990 / >(Of course, if this was a common enough operation, "+" would be better >defined as > +: Like Self x Integer --> Like Self >or > +: Like Self x Integer --> Integer.) I have run across a similar desire in C++ for a "same type as receiver" type expression in constructing collection classes. For example, I would like to express the fact that all collections can return a copy of themselves. Naturally, I would like to put this declaration in the same class in which I express the other fundamental collection operations, such as, for example, insertion of elements or enumeration. This is in class Collection. Thus, I am tempted to write: class Collection { public: virtual Collection* Copy(); }; but of course, this gives the incorrect return type, since expressions of the form class Set : public Collection { /* ... */ }; //... Set* pset = pSomeOtherSet->Copy(); give a type error. What I really want to say is something like class Collection { public: virtual typeof(*this)* Copy(); }; Though I have only thought about it in briefly, I believe this interacts cleanly with the automatic type conversions of Derived* to Base*. It strikes me that the typeof() operator might be generally useful in other situations as well. One might ask if parameterized types would allow us to express this construction. I don't see how the current parameterized type proposal would accomplish it. Bob Atkinson Microsoft