Path: utzoo!attcan!uunet!samsung!umich!sharkey!amara!des From: des@amara.uucp (Dave Steinhoff) Newsgroups: comp.lang.c++ Subject: Reference pointer function arguments baroquen? Message-ID: Date: 25 May 90 03:22:59 GMT Sender: news@adi.COM Distribution: na Organization: /var/news/organization Lines: 65 I ran into this case in a library someone else developed with a C++1.2 version of cfront. I am using SUNC++2.0. The authors apparently got a different result than I am getting, because their software works. I apologize if this has been discussed before. In the program listed below, the compiler warns me: "conversion of lvalue needed: temporary used to initialize reference" on the call 'f(i2);' It does indeed make a local copy of the pointer, which I don't understand. I would have expected it to be handled as a reference parameter with the actual argument's address passed through, even though it is a pointer to a derived class. When I change the call to 'f((Int*)i2);' as shown in the comment below, the warning message goes away, but the same behavior occurs. Can anyone explain this, or is the subclass-pointer-reference mechanism baroquen... ------------------------------ #include class Int {}; class Int2 : public Int {}; void f (Int *& i) { i = new Int;} void f2 (Int2 *& i) { i = new Int2;} void f3 (Int **i) { *i = new Int;} main () {Int2* i2 = new Int2; Int * i1 = new Int; cout << (long) i2 << " " << (long) i1 << "\n"; f (i2); f(i1); // f(i2) -> f((Int*)i2; cout << (long) i2 << " " << (long) i1 << "\n"; f3 ((Int**)&i2); cout << (long) i2 << "\n"; f2(i2); cout << (long) i2 << "\n"; } /* ------------------------------ Output with f(i2) and f((Int*)i2) is identical: 139832 139840 139832 140884 140892 140900 ------------------------------ */ --------------------------------------------- Cheers, Dave -- ------------------------------------------------------------------- Dave Steinhoff Applied Dynamics, Int'l des@amara.UUCP 3800 Stone School Rd. des@adi.com Ann Arbor, Mi 48108 ...uunet!amara!des (313)973-1300 -------------------------------------------------------------------