On Aug 4, 10:03 pm, trade...@yahoo.com wrote:
Quote:
I am playing with boost pointer and try to wrap the following codes
>
A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;
}
Quote:
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
Are you sure? I've not much experience with boost::shared_ptr,
but all of the smart pointers I've use do accept constructing
them with NULL.
Quote:
How should I do it in this case?
In this particular case, I'd probably write something like:
shared_ptr< A >
func()
{
return shared_ptr< A >(
someCondition
? new A
: NULL ) ;
}
In more complicated cases, however:
shared_ptr< A >
func()
{
shared_ptr< A result ;
if ( someCondition ) {
result = new A ;
}
return result ;
}
I'd be very surprised if either of them failed to work with
boost::shared_ptr. Just as I'd be very surprised if, given the
definition of result, above "result = NULL ;" wouldn't compile;
for that matter, there's no reason why "if ( result == NULL )"
can't be made to work as expected, either.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34