From: Ulrich Eckhardt on
Gerry Hickman wrote:
>>> void PopulateStrings(vector<string> * guids)
>>> {
>>> guids->clear();
>>> guids->push_back("test1");
>>> guids->push_back("test2");
>>> }
>>
>> While this code will work, there is one thing I object to: in C++, where
>> you have references, a pointer[1] means to me that something is optional,
>> i.e. passing zero is okay, but you don't mean that. Still, you must
>> handle that case, so either you just return (making it a non-error),
>> throw an exception (making it a runtime error) or use assert() (making it
>> a programmer's error).
>
> Does this only apply to the 'pointer' version, or does it apply to the
> 'references' version too?

A reference can't be null, so this doesn't apply.

Uli

From: David Wilkinson on
Gerry Hickman wrote:
> I guessed 'returns' should be a comment, but the first two lines didn't
> make sense with void following vector<string>::size_type, I guess I was
> supposed to _replace_ void with vector<string>::size_type, seems obvious
> now.

> Does this only apply to the 'pointer' version, or does it apply to the
> 'references' version too?

Gerry:

The "void" is Doug's post was a typo, I think. This confused me also.

When you pass a pointer, the "implication" is that it could be NULL, and
that the function will take some special action in this case. If you
pass a reference, it cannot be NULL (every reference must be initialized
to some object), so no such action need be taken.

Generally, in C++ references are preferred to pointers.

--
David Wilkinson
Visual C++ MVP
From: Ben Voigt [C++ MVP] on

"Ulrich Eckhardt" <eckhardt(a)satorlaser.com> wrote in message
news:tp06p4-ile.ln1(a)satorlaser.homedns.org...
> Gerry Hickman wrote:
>>>> void PopulateStrings(vector<string> * guids)
>>>> {
>>>> guids->clear();
>>>> guids->push_back("test1");
>>>> guids->push_back("test2");
>>>> }
>>>
>>> While this code will work, there is one thing I object to: in C++, where
>>> you have references, a pointer[1] means to me that something is
>>> optional,
>>> i.e. passing zero is okay, but you don't mean that. Still, you must
>>> handle that case, so either you just return (making it a non-error),
>>> throw an exception (making it a runtime error) or use assert() (making
>>> it
>>> a programmer's error).
>>
>> Does this only apply to the 'pointer' version, or does it apply to the
>> 'references' version too?
>
> A reference can't be null, so this doesn't apply.

That's not true. It's illegal to use a null reference, but in the same way
that it's illegal to use a null pointer.


From: Ben Voigt [C++ MVP] on

"David Wilkinson" <no-reply(a)effisols.com> wrote in message
news:eR1GUla3HHA.4584(a)TK2MSFTNGP03.phx.gbl...
> Gerry Hickman wrote:
>> I guessed 'returns' should be a comment, but the first two lines didn't
>> make sense with void following vector<string>::size_type, I guess I was
>> supposed to _replace_ void with vector<string>::size_type, seems obvious
>> now.
>
>> Does this only apply to the 'pointer' version, or does it apply to the
>> 'references' version too?
>
> Gerry:
>
> The "void" is Doug's post was a typo, I think. This confused me also.
>
> When you pass a pointer, the "implication" is that it could be NULL, and
> that the function will take some special action in this case. If you pass
> a reference, it cannot be NULL (every reference must be initialized to
> some object), so no such action need be taken.

This "every reference must be initialized to some object" is not true. Any
pointer can be converted to a reference.

>
> Generally, in C++ references are preferred to pointers.
>
> --
> David Wilkinson
> Visual C++ MVP


From: Bo Persson on
Ben Voigt [C++ MVP] wrote:
:: "Ulrich Eckhardt" <eckhardt(a)satorlaser.com> wrote in message
:: news:tp06p4-ile.ln1(a)satorlaser.homedns.org...
::: Gerry Hickman wrote:
:::::: void PopulateStrings(vector<string> * guids)
:::::: {
:::::: guids->clear();
:::::: guids->push_back("test1");
:::::: guids->push_back("test2");
:::::: }
:::::
::::: While this code will work, there is one thing I object to: in
::::: C++, where you have references, a pointer[1] means to me that
::::: something is optional,
::::: i.e. passing zero is okay, but you don't mean that. Still, you
::::: must handle that case, so either you just return (making it a
::::: non-error), throw an exception (making it a runtime error) or
::::: use assert() (making it
::::: a programmer's error).
::::
:::: Does this only apply to the 'pointer' version, or does it apply
:::: to the 'references' version too?
:::
::: A reference can't be null, so this doesn't apply.
::
:: That's not true. It's illegal to use a null reference, but in the
:: same way that it's illegal to use a null pointer.

There aren't any null references, only invalid ones. Like a reference
to an object that has been destroyed.

If someone passes an invalid reference to my functions, that is a bug
in their code, not my problem. If someone passes a null pointer, I
must handle that.


Bo Persson