Mr A wrote:
> Hi!
> I've been thinking about passing parameteras using references instead
> of pointers in order to emphasize that the parameter must be an
> object.
>
> Exemple:
> void func(Objec& object); //object must be an object
>
> instead of
>
> void func(Object* object); //object can be NULL
>
> I belive that this is a good idea since , in the reference case, it's
> clear that NULL is not an option. It's also clear that NULL is an
> option when a pointer is expected (stating the obvious
). The code
> becomes somewhat more self-documenting.
>
> Any comments on why this could be a bad idea or do you think it's just
> a matter of taste?
>
> /H
Unfortunately, there are different vantage points from which we view a
function: declaration, definition and invocation. Each has it's own
exposed and hidden information. It has been argued that passing a pointer
is more explicit, and makes the code easier to read. I tend to agree.
AFAIK, you *can* pass a null to func() in your example, and the compiler
will accept it. Your code will segfault when you do so. If you pass a
pointer, you can check for null, before accessing it. I, therefore,
suggest using a pointer instead of a reference.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell