Meryl Silverburgh | 16 Jul 02:06

Re: how can I return a 'null' smart pointer?

On Tue, Jul 15, 2008 at 3:26 AM, Thorsten Ottosen
<thorsten.ottosen <at> dezide.com> wrote:
> Marshall Clow skrev:
>>
>> At 4:43 PM -0500 7/14/08, Meryl Silverburgh wrote:
>>>
>>> Hi,
>>>
>>> Can you please tell me how can I return a 'null' smart pointer?
>>>
>>> typedef shared_ptr<MyA> MyAPtr;
>>> typedef vector<MyAPtr>  MyAVector;
>>>
>>> For example,I have this code, which return NULL in an error situation:
>>>
>>> MyAPtr myfunction(MyAVector& v, int size) {
>>>      if (index < v.size()) {
>>>            return v[index];
>>>       } else {
>>>          // how can I return NULL here?
>>>      }
>>> }
>>>
>>> How can I achieve that when I use boost smart pointer?
>>
>> I suggest that you throw an exception here, rather than returning some
>> bogus value.
>

You mention "I suggest that you throw an exception here, rather than
returning some bogus value."

So when I do this:

shared_ptr<MyClass> aPtr;

and then I check for 'null' , like this:

if (aPtr == NULL) {
// will it return true here?

}

> I concur, but I suggest you simply add a precondition:
>
>  assert( index < v.size() )
>
> -Thorsten
> _______________________________________________
> Boost-users mailing list
> Boost-users <at> lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

Gmane