Re: Pointers vs References: A Question on Style

From: Phlip (phlip_cpp_at_yahoo.com)
Date: 07/26/04


Date: Sun, 25 Jul 2004 23:45:48 GMT

DaKoadMunky wrote:

> >There are 2 and only 2 situations in which I choose a pointer over a
> >reference:
> >
> >1) When it must be re-seated
> >
> >2) When arrays are involved
>
> Another possibility is when a function argument is optional. Pointers can
be
> null whereas references cannot.

One good style rule is to never return null, and never accept null as
parameter.

Follow that rule by passing in instead a Null Object. Consider this code:

void funk(SimCity const * pCity)
{
      if (pCity)
          pCity->throwParade();
}

Now contrast with this:

class NullCity: public SimCity
{
  public: /*virtual*/ void throwParade() {}
};

void funk(SimCity const & aCity)
{
      aCity.throwParade();
}

The code simplifies because it pushes behavior behind an interface. The
interface simply promises to its caller that it is doing something, whether
or not it really is. Patterns like this are the heart of OO - programming to
the interface instead of the implementation.

Desmond Liu wrote:

> But why use a reference? Is there any inherent advantage of using a
> reference over using a pointer? My workplace discourages the use of
> pointers when it comes to parameter passing. They always prefer
references.

You need to pair-program with your colleagues, because if they know just
enough C++ to write that advice down, they probably know much more verbally.

The C++ keyword const instructs compilers to reject overt attempts to change
a variable's value. Covert attempts produce undefined behavior, meaning
anything could happen.

C++ functions can take arguments by copy, by address, or by reference.
Ideally, if an object passed into a function does not change, the object
should pass by copy:

void foo(SimCity aCity);

That code is inefficient. In general, programmers should not stress about
efficiency until they have enough code to measure it and find the slow
spots. In this situation, a more efficient implementation is equal cost.
When we pass by reference, our program spends no time making a huge copy of
an entire city:

void foo(SimCity &aCity);

Now if foo() won't change that city's value, the function should declare
that intention in its interface, using pass-by-constant-reference to
simulate pass-by-copy:

void foo(SimCity const &aCity);

That is the most efficient call syntax, cognitively and physically. It's
cognitively efficient because it gives foo() no copied object to foolishly
change and then discard. Statements inside foo() that might attempt to
change that city shouldn't compile. It's physically efficient because the
compiler produces opcodes that only give foo() a handle to an existing city,
without copying it.

C++ supports qualifications before their qualified types, such as "const
SimCity &". I try to write expressions with the most important part first.
There are also subtle technical reasons, in rare situations, to write
"SimCity const &", with the const after its type.

-- 
  Phlip
  http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces


Relevant Pages

  • Re: constructor initialization list
    ... consult a reference. ... For example, I know how "const" works with pointers, ie.: ... But then when it comes to pointers to pointers, I'm not sure how the whole ... *not* guess (similarly with the constructor order scenario), ...
    (comp.lang.cpp)
  • Re: Is CArray best for this...
    ... If CArray::GetAtreturns a reference to the actual object then there should be no problem, but if GetAt returns a copy, then that would explain everything - I am using a pointer to a temporary copy of something that goes of scope immediately after I setup a pointer to it and the space it occupies may or may not be overwritten in memory. ... TYPE operator (int nIndex) const; ... As to your problem, I think AliR is correct; if you want to hold pointers to things in a container like CArray then you need to hold pointers in the container, because otherwise the pointers can be invalidated by reallocation. ...
    (microsoft.public.vc.mfc)
  • Re: handle/reference?
    ... > void MyFunction(const HANDLE myhandle) ... > void MyFunction ... If you pass a handle by reference, ... const reference or const, you're inhibiting such changes. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: discussing C evolution
    ... With pointers, the difference is always ... will never change object, because it does not take a pointer. ... it takes a normal parameter or a reference. ... called function must have accepted it as a const reference and can't ...
    (comp.lang.c)
  • Re: Is it possible to assign a default value...great, but how?
    ... > much as NULL does it with pointers". ... read your posts in Google Groups but as one reason why people don't normally ... > could call fin the case of a reference argument too. ... void f ...
    (microsoft.public.vc.language)