Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!rochester!kodak!ektools!randolph From: randolph@ektools.UUCP (Gary L. Randolph) Newsgroups: comp.lang.c++ Subject: Re: Return types of virtual member functions Message-ID: <1930@ektools.UUCP> Date: 2 Jun 89 13:40:09 GMT References: <3403@hplabsz.HPL.HP.COM> Sender: randolph@ektools (Gary L. Randolph) Reply-To: randolph@ektools.UUCP (Gary L. Randolph) Organization: Eastman Kodak, Dept. 47, Rochester NY Lines: 115 In article <3403@hplabsz.HPL.HP.COM> bs@hplabsz.HPL.HP.COM (Bob Shaw) writes: > >I'm trying to build a "bushy" single inheritance tree. Many of the child >classes define a few member functions beyond those of the parent. Most of >the system deals with the various objects at the highest level of >abstraction (i.e. only use the parent member functions), but parts of the >system change the level of abstraction and deal with the object as an >instance of a child class. > >My question involves the return value types in virtual member functions. >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. >Obviously, children of such a child would be constrained to return a pointer >to child or something derived from it. > >The code below does not work as I had hoped. Can someone involved in the ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ It does now. Will I do? >ATT C++ 2.0 release tell me if this will work in 2.0 and, if not, whether >it's a bug or why it isn't a reasonable thing to do? ^^^^ ^^^^^^^^^^^^^^^^^^^^^^ NOT BUG See below > > >Thanks, No prob >bs <- obviously not the att one cute > /* ---------------------------- Output from following code: <----Modified from your original parent->msg() parent::msg entered child->msg() child::msg entered child_as_parent->msg() child::msg entered <-----As you wished ---------------------------- */ #include class parent { public: virtual parent* msg(); }; class child : public parent { public: virtual parent* msg(); }; parent* parent::msg() { cout << " parent::msg entered\n"; return this; } parent* child::msg() { cout << " child::msg entered\n"; return this; } void main() { parent* presult; parent* cresult; parent* p = new parent; parent* c = new child; cout << "\nparent->msg()\n"; presult = p->msg(); cout << "\nchild->msg()\n"; cresult = c->msg(); cout << "\nchild_as_parent->msg()\n"; p = c; presult = p->msg(); } All that was changed was the pointer types of c, the return type of child::msg(), and cresult. You seem to be close to understanding virtual functions except that you need to understand that it is the *object* being pointed to that makes the assignment to a base pointer work. You want the (modified) pointers above to be of type parent. You can then have the parent pointers point to child objects. I believe that once you look this over, the pointer types will make sense. Gary