home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: Reference as a return type
- Message-ID: <rmartin.715995051@thor>
- Date: 8 Sep 92 23:30:51 GMT
- References: <1992Sep7.185350.3349@access.digex.com>
- Sender: news@Rational.COM
- Lines: 33
-
- car@access.digex.com (Mr. Blue) writes:
-
-
- |I'm probably asking a question that belongs in FAQ, (and if there
- |is such a list of question for this newsgroup I'd be grateful if someone
- |emailed it to me)
-
- |Anyway my question is this: Whats the difference between returning a
- |pointer to a class and a reference to a class from a member function??
- |I mean a reference is just a pointer, isn't it? However I get the feeling
- |that the semantics are very different, i.e. copy constructors are called,
- |etc. Can someone tell me what happens on the run time stack to contrast
- |the 2 cases? I know this is probably a naieve question,but I'm still
- |learning so forgive me.
-
-
- A reference is an lvalue. A pointer is an rvalue. Thus, you can use
- a member function which returns a reference as the target of an
- assignment.
-
- Also, because references are lvalues, it is illegal for them to be
- invalid, they must always refer to a valid object. (The language
- specifies this as a rule, but most compilers can't enforce it) Thus,
- when a member function returns a pointer, it might be zero, so you
- should test is. But if it returns a reference, then you "know" that
- it is valid and can use it without the test.
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-