[comp.lang.c++] Return types of virtual member func

johnson@p.cs.uiuc.edu (05/29/89)

/* Written  8:01 pm  May 26, 1989 by bs@hplabsz.HPL.HP.COM in comp.lang.c++ */
If a parent class defines a virtual member function such as

 virtual parent* msg();

I would like a child (derived) class to be able to define its version of the
member function as

 virtual child* msg();     rather than    virtual parent* msg();.

It's my understanding that a child* can always be assigned to something
defined as a parent*, so I can't see why this should break the type system.
/* End of text from p.cs.uiuc.edu:comp.lang.c++ */

I'm not going to answer the question, except to agree that this example
SHOULD work and that it DOESN'T work.  However, if it did work then you 
would find that you would want a member function of parent that would take
a pointer to a parent, while the child would restrict this to a pointer
to a child.  There is no way that this can be type-correct, since code in
the parent might try to call the function with a pointer to the parent.
However, the need to do this is very common.

One of my students is building a framework for network protocol handlers
in C++.  He wanted to have a Conduit class and a Message class, with 
derived classes TCP_Conduit, IP_Conduit, TCP_Message and IP_Message.
We decided to use a facility in GNU C++ for building parameterized classes,
which are really macros like Ada generics.  You can use the C preprocessor
to do this as well.  Thus, Conduit was parameterized by a type T.
TCP_Conduit was derived from a version of Conduit that was instantiated
with T equal to TCP_Message.  All the member functions in Conduit
referred to T instead of to Message, so they all got instantiated as
TCP_Message.

Parameterized classes are badly needed in C++.  You can get around it
with macros, as I described above, but it's a kludge.  Stroustrup wrote
a paper on how to add parameterized classes to C++, but as far as I
know, it has never been implemented.

Ralph Johnson