Daryle Walker | 17 Jul 12:02
Picon

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

On Jul 16, 2008, at 6:24 PM, Joshua Perry wrote:

> A function like you mention (maybe vector<OrderPtr>  
> Customer::GetUnfulfilledOrders( void ); )  could return an vector  
> or custom collection with 0 elements, however with a function like  
> OrderPtr Customer::GetOrder(int ordern); to me it feels natural to  
> throw an exception.

I think this is more like a find function, so you could return an  
iterator to the appropriate element, and return a one-past-the-end  
iterator if the order number wasn't found.  In your example, you're  
effectively trying to run a find and then return a dereferenced  
result.  I guess that you are trying to hide any container-ness about  
the order system, so either returning an order reference or throwing  
is probably better, but only if you expect the user to never try  
entering invalid order numbers.  If entering invalid numbers is a  
standard occurrence (pre-checking is too hard and/or not expected),  
then it wouldn't be a post-condition violation, and a null-pointer  
should be returned instead.

--

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

Gmane