home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:16803 gnu.g++.help:1481 gnu.gcc.help:2585
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news.service.uci.edu!network.ucsd.edu!ucsbcsl!foxtrot!doug
- From: doug@foxtrot.ccmrc.ucsb.edu (Douglas Scott)
- Newsgroups: comp.lang.c++,gnu.g++.help,gnu.gcc.help
- Subject: Passing pointers as references-to-pointers?
- Keywords: reference,pointer
- Message-ID: <6856@ucsbcsl.ucsb.edu>
- Date: 23 Nov 92 22:36:25 GMT
- Sender: root@ucsbcsl.ucsb.edu
- Followup-To: comp.lang.c++
- Organization: Center for Computer Music Research and Composition, UCSB
- Lines: 36
-
- Putting aside whether or not this is good practice, should the following code
- work?
-
- class BaseClass;
- class DerivedClass; // assume this is derived from the former
-
- typedef BaseClass * &BasePtrRef; // reference-to-pointer type
-
- void fun1(BaseClass *b) {
- // whatever
- }
-
- void fun2(BasePtrRef bpr) {
- fun1(bpr); // passed down as if a regular pointer
- }
-
- main() {
- DerivedClass *dc = new DerivedClass;
- fun2(dc); // passing pointer-to-Derived as reference-to-pointer-to-Base
- }
-
- In g++ 2.2.2, using gdb to check the code, the internal structure of the
- BaseClass portion of dc (including the virtual function table) does not survive
- being passed into fun1. The address of the pointer is the same, but the
- contents are wrong. Now, this might just be a gdb problem, or a g++ bug, but
- I wanted to just verify that I should expect any operation on 'b' in fun1 to
- work, assuming those operations are defined for BaseClass.
-
- Am I missing some detail, or should this work?
-
- Thanks!
- --
- Douglas Scott (805)893-8352
- Center for Computer Music Research and Composition
- University of California, Santa Barbara
- Internet: (NeXTMail ok) <doug@foxtrot.ccmrc.ucsb.edu>
-