Re: STL Vector - pass by reference?
Ben Voigt [C++ MVP] wrote:
:: "Ulrich Eckhardt" <eckhardt@satorlaser.com> wrote in message
:: news:tp06p4-ile.ln1@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