[comp.lang.c++] inheritance and `type loss'

bobatk@microsoft.UUCP (Bob ATKINSON) (07/17/90)

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

johnb@srchtec.UUCP (John Baldwin) (07/18/90)

In article <55845@microsoft.UUCP> bobatk@microsoft.UUCP (Bob ATKINSON) writes:
>
>It strikes me that the typeof() operator might be generally useful
>in other situations as well.

Definitely!  I can think of a lot of places where I've wanted typeof(), and
its even more desirable in ANSI C (so you can write "smart" macros).
In fact, in one of his recent articles about the ANSI C standardization
process, P. J. Plauger wrote saying that typeof() was one of the things
he really wished had been implemented.  [Quick! Somebody run that sentence
through a grammar corrector!]  :-)

Maybe the user community can "encourage" the ANSI C++ committee to consider
adding typeof() to the language.

-- 
John T. Baldwin			| The opinions expressed herein are available
research analyst		| for the small price of $5.
search technology, inc.		| Operators are online 24 hours a day; call
johnb@srchtec.uucp		| 1-800-366-2277 (1-800-FOO-BARR).