home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: pjl@sparc2.cs.uiuc.edu (Paul Lucas)
- Subject: Re: easy reference question
- Message-ID: <1992Aug26.003253.8479@sunb10.cs.uiuc.edu>
- Sender: news@sunb10.cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- References: <1992Aug24.123549.28952@jho.com>
- Distribution: usa
- Date: Wed, 26 Aug 1992 00:32:53 GMT
- Lines: 48
-
- In <1992Aug24.123549.28952@jho.com> harrison@jho.com (Mark Harrison) writes:
-
- >We have a member function of a class that is used to set a new
- >destination for the class.
-
- >class foo {
- > CallManager * caller;
- >public:
- > setcaller(CallManager * newcaller) { caller = newcaller; }
- >};
-
- >We have also received a suggestion that we convert the pointers
- >to references. At this point I'm a little confused... If I
- >declare
-
- >class foo {
- > CallManager& caller;
- >public:
- > setcaller(CallManager& newcaller) { caller = newcaller; }
- >};
-
- >it seems that when setcaller(x) is called, the actual data object
- >will be assigned rather than the address (or reference), which is
- >what we want.
-
- ****> That's right; a reference can only be initialized once; note:
- I said _initialized_, not assigned. After that the object
- referred to is _always_ used, never the reference; a reference
- is an alias for another object.
-
- >Should we say "caller = &newcaller;" ? Is this an exception to
- >the rule "use references instead of pointers whenever possible."?
-
- *****> I don't know where you got that rule from.
- BTW, your example using the reference is illegal C++; a
- reference _must_ be initialized in all of the classes
- constructors:
-
- class foo {
- CallManager &caller;
- public:
- foo( ... ) : caller( ... ) { ... }
- // ...
- };
- --
- - Paul J. Lucas University of Illinois
- AT&T Bell Laboratories at Urbana-Champaign
- Naperville, IL pjl@cs.uiuc.edu
-