home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16803 < prev    next >
Encoding:
Text File  |  1992-11-24  |  1.7 KB  |  50 lines

  1. Xref: sparky comp.lang.c++:16803 gnu.g++.help:1481 gnu.gcc.help:2585
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news.service.uci.edu!network.ucsd.edu!ucsbcsl!foxtrot!doug
  3. From: doug@foxtrot.ccmrc.ucsb.edu (Douglas Scott)
  4. Newsgroups: comp.lang.c++,gnu.g++.help,gnu.gcc.help
  5. Subject: Passing pointers as references-to-pointers?
  6. Keywords: reference,pointer
  7. Message-ID: <6856@ucsbcsl.ucsb.edu>
  8. Date: 23 Nov 92 22:36:25 GMT
  9. Sender: root@ucsbcsl.ucsb.edu
  10. Followup-To: comp.lang.c++
  11. Organization: Center for Computer Music Research and Composition, UCSB
  12. Lines: 36
  13.  
  14. Putting aside whether or not this is good practice, should the following code
  15. work?
  16.  
  17. class BaseClass;
  18. class DerivedClass;    // assume this is derived from the former
  19.  
  20. typedef BaseClass * &BasePtrRef;    // reference-to-pointer type
  21.  
  22. void fun1(BaseClass *b) {
  23.     // whatever
  24. }
  25.  
  26. void fun2(BasePtrRef bpr) {
  27.     fun1(bpr);    // passed down as if a regular pointer
  28. }
  29.  
  30. main() {
  31.     DerivedClass *dc = new DerivedClass;
  32.     fun2(dc); // passing pointer-to-Derived as reference-to-pointer-to-Base
  33. }
  34.  
  35. In g++ 2.2.2, using gdb to check the code, the internal structure of the
  36. BaseClass portion of dc (including the virtual function table) does not survive
  37. being passed into fun1.  The address of the pointer is the same, but the 
  38. contents are wrong.  Now, this might just be a gdb problem, or a g++ bug, but 
  39. I wanted to just verify that I should expect any operation on 'b' in fun1 to 
  40. work, assuming those operations are defined for BaseClass.
  41.  
  42. Am I missing some detail, or should this work?
  43.  
  44. Thanks!
  45. -- 
  46. Douglas Scott                              (805)893-8352
  47. Center for Computer Music Research and Composition
  48. University of California, Santa Barbara
  49. Internet: (NeXTMail ok)   <doug@foxtrot.ccmrc.ucsb.edu>
  50.