home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13409 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  1.7 KB

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