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

  1. Path: sparky!uunet!UB.com!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointer or Reference Arguments
  5. Message-ID: <rmartin.715871185@thor>
  6. Date: 7 Sep 92 13:06:25 GMT
  7. References: <1992Sep7.075249.22855@news.uni-stuttgart.de>
  8. Sender: news@Rational.COM
  9. Lines: 42
  10.  
  11. KOCHER@141.58.123.1 (HARTMUT KOCHER) writes:
  12.  
  13. |I am currently trying to find a consistent style in 
  14. |using pointer and references as function arguments.
  15. |Currently, I am thinking about two different styles that 
  16. |I want to explain in the following paragraphs:
  17.  
  18. |1) Reference arguments are used only when aliasing 
  19. |occurs and the parameter is used only during the 
  20. |lifetime of the function. Pointers are used whenever the 
  21. |function might keep a copy of the pointer for later use, 
  22. |e.g., store it in a private list.
  23.  
  24. |2) Reference arguments are used whenever you must supply 
  25. |a valid object to the function. Pointers are used when 0 
  26. |might be a valid parameter as well.
  27.  
  28. I use style 2) exclusively.  I use a reference parameter to indicate
  29. to the caller that I expect a valid object, and that i am not going to
  30. test it for zero.  I use a pointer argument if the argument can be
  31. zero, (works well with default arugments)
  32.  
  33. The same goes for return values and elements in structures.  If they
  34. are pointers, they can be zero and must be tested.  If they are
  35. refereces, my convention says that they must exist, and need not be
  36. tested for zero.
  37.  
  38. In C++ a zero reference is illegal, (Although many compilers don't
  39. check for it.)  And a reference in a structure or class must be bound
  40. at initialization.  So the guarantee that a reference indicates that
  41. an object is present is a reasonable one.
  42.  
  43. My objection to style 1) is that it must compete with zero pointers.
  44. i.e. an argument which qualifies as a reference under fule 1) might
  45. still have to be a pointer if it can be zero...
  46.  
  47.  
  48. --
  49. Robert Martin                        Training courses offered in:
  50. R. C. M. Consulting                       Object Oriented Analysis
  51. 2080 Cranbrook Rd.                        Object Oriented Design
  52. Green Oaks, Il 60048 (708) 918-1004       C++
  53.