home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12885 < prev    next >
Encoding:
Text File  |  1992-08-25  |  1.9 KB  |  61 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!ux1.cso.uiuc.edu!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc2.cs.uiuc.edu!pjl
  3. From: pjl@sparc2.cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: easy reference question
  5. Message-ID: <1992Aug26.003253.8479@sunb10.cs.uiuc.edu>
  6. Sender: news@sunb10.cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <1992Aug24.123549.28952@jho.com>
  9. Distribution: usa
  10. Date: Wed, 26 Aug 1992 00:32:53 GMT
  11. Lines: 48
  12.  
  13. In <1992Aug24.123549.28952@jho.com> harrison@jho.com (Mark Harrison) writes:
  14.  
  15. >We have a member function of a class that is used to set a new
  16. >destination for the class.
  17.  
  18. >class foo {
  19. >    CallManager * caller;
  20. >public:
  21. >    setcaller(CallManager * newcaller) { caller = newcaller; }
  22. >};
  23.  
  24. >We have also received a suggestion that we convert the pointers
  25. >to references.  At this point I'm a little confused... If I
  26. >declare
  27.  
  28. >class foo {
  29. >    CallManager& caller;
  30. >public:
  31. >    setcaller(CallManager& newcaller) { caller = newcaller; }
  32. >};
  33.  
  34. >it seems that when setcaller(x) is called, the actual data object
  35. >will be assigned rather than the address (or reference), which is
  36. >what we want.
  37.  
  38. ****>    That's right; a reference can only be initialized once; note:
  39.     I said _initialized_, not assigned.  After that the object
  40.     referred to is _always_ used, never the reference; a reference
  41.     is an alias for another object.
  42.  
  43. >Should we say "caller = &newcaller;" ?  Is this an exception to
  44. >the rule "use references instead of pointers whenever possible."?
  45.  
  46. *****>    I don't know where you got that rule from.
  47.     BTW, your example using the reference is illegal C++; a
  48.     reference _must_ be initialized in all of the classes
  49.     constructors:
  50.  
  51.         class foo {
  52.             CallManager &caller;
  53.         public:
  54.             foo( ... ) : caller( ... ) { ... }
  55.             // ...
  56.         };
  57. -- 
  58.     - Paul J. Lucas                University of Illinois    
  59.       AT&T Bell Laboratories        at Urbana-Champaign
  60.       Naperville, IL            pjl@cs.uiuc.edu
  61.