home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!UB.com!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: Pointer or Reference Arguments
- Message-ID: <rmartin.715871185@thor>
- Date: 7 Sep 92 13:06:25 GMT
- References: <1992Sep7.075249.22855@news.uni-stuttgart.de>
- Sender: news@Rational.COM
- Lines: 42
-
- KOCHER@141.58.123.1 (HARTMUT KOCHER) writes:
-
- |I am currently trying to find a consistent style in
- |using pointer and references as function arguments.
- |Currently, I am thinking about two different styles that
- |I want to explain in the following paragraphs:
-
- |1) Reference arguments are used only when aliasing
- |occurs and the parameter is used only during the
- |lifetime of the function. Pointers are used whenever the
- |function might keep a copy of the pointer for later use,
- |e.g., store it in a private list.
-
- |2) Reference arguments are used whenever you must supply
- |a valid object to the function. Pointers are used when 0
- |might be a valid parameter as well.
-
- I use style 2) exclusively. I use a reference parameter to indicate
- to the caller that I expect a valid object, and that i am not going to
- test it for zero. I use a pointer argument if the argument can be
- zero, (works well with default arugments)
-
- The same goes for return values and elements in structures. If they
- are pointers, they can be zero and must be tested. If they are
- refereces, my convention says that they must exist, and need not be
- tested for zero.
-
- In C++ a zero reference is illegal, (Although many compilers don't
- check for it.) And a reference in a structure or class must be bound
- at initialization. So the guarantee that a reference indicates that
- an object is present is a reasonable one.
-
- My objection to style 1) is that it must compete with zero pointers.
- i.e. an argument which qualifies as a reference under fule 1) might
- still have to be a pointer if it can be zero...
-
-
- --
- 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++
-