Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!rutgers!gatech!mcnc!thorin!luther!kelleymt From: kelleymt@luther.cs.unc.edu (Michael T. Kelley) Newsgroups: comp.lang.c++ Subject: Implementing virtual functions that return reference to self Keywords: virtual functions, derived classes Message-ID: <8975@thorin.cs.unc.edu> Date: 26 Jul 89 16:13:42 GMT Sender: news@thorin.cs.unc.edu Lines: 37 I've been working on a set of classes for manipulating graphics primitives. For many of the sub-classes, there are members I don't want to "stub-out" in the parent class. The problem comes when grouping redefined functions in the sub-class with functions not stubbed out in the parent into a single expression: #include class Base { public: Base() { } virtual Base& hello() { cout << "hello "; return *this; } }; class Derived : public Base { public: Derived() { } Base& hello() { cout << "g'day "; return *this; } // ugh. void goodbye() { cout << "see ya!\n" } }; main() { Derived d; d.hello().goodbye(); // ERROR: goodbye undefined } In this case, I know I'm dealing with a Derived, so I'd like to be able to use goodbye(). Can someone explain the harm in allowing hello() to return a Derived& in the Derived class? Or is there an implementation issue lurking underneath? Michael T. Kelley University of North Carolina at Chapel Hill kelleymt@cs.unc.edu uunet!mcnc!unc!kelleymt (919) 962-1761