Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!cimshop!davidm From: cimshop!davidm@uunet.UU.NET (David S. Masterson) Newsgroups: comp.lang.c++ Subject: Re: Reference problem?? Message-ID: Date: 25 Apr 91 16:59:10 GMT References: Sender: davidm@cimshop.UUCP Distribution: comp Organization: Consilium Inc., Mountain View, California Lines: 71 In-reply-to: cimshop!davidm@uunet.UU.NET's message of 25 Apr 91 06:35:47 GMT >>>>> On 25 Apr 91 06:35:47 GMT, [I] said: Me> #ifdef GPLUSPLUS Me> #include Me> #else Me> #include Me> #include Me> #endif Me> class alpha { // top of the hierarchy Me> int value1; Me> public: Me> alpha(int a) : value1(a) {} Me> virtual void display() { Me> cout << "Alpha value1 " << value1 << "\n"; Me> } Me> }; Me> class beta : public alpha { // middle of the hierarchy Me> int value2; Me> public: Me> beta(int a, int b) : alpha(a), value2(b) {} Me> virtual void display() { Me> alpha::display(); Me> cout << "Beta value2 " << value2 << "\n"; Me> } Me> }; Me> class gamma : public beta { // bottom of the hierarchy Me> int value3; Me> public: Me> gamma(int a, int b, int c) : beta(a, b), value3(c) {} Me> virtual void display() { Me> beta::display(); Me> cout << "Gamma value3 " << value3 << "\n"; Me> } Me> }; Me> void testfunc(alpha*& parm) { Me> cout << "testfunc display\n"; Me> parm = new alpha(40); Me> parm->display(); Me> } Me> int main() { Me> gamma g(10, 20, 30); Me> cout << "first display\n"; Me> g.display(); Me> cout << "second display\n"; Me> gamma *gptr = &g; Me> gptr->display(); Me> testfunc((alpha*) gptr); // ^-- why is the cast necessary? Me> cout << "third display\n"; Me> gptr->display(); Me> } Me> // End of the program I don't understand why the cast is necessary. Without the cast, Oregon C++, Turbo C++, and G++ tells me something like: Warning: Lvalue coerced into temporary to initialize reference type Can anyone explain why? -- ==================================================================== David Masterson Consilium, Inc. (415) 691-6311 640 Clyde Ct. uunet!cimshop!davidm Mtn. View, CA 94043 ==================================================================== "If someone thinks they know what I said, then I didn't say it!"