Yes, but returning a pointer from foo is a bad progamming practice.
Says who?
Sometimes you don't have a choice.
Why don't you just say that auto_ptr is deprecated ?
You are concentrating too much on returning pointers.
void bar( int Type )
{
Obj* pObj;
pObj = Factory.CreateObject( Type );
// do something
// object no longer needed, delete it.
delete pObj;
}
Now lets say, that the part // do something is rather complicated and there
is a chance that things might go wrong. You insert some tests and do a return.
But when doing so you must not forget do delete the object given to you
from the factory.
void bar( int Type )
{
Obj* pObj;
pObj = Factory.CreateObject( Type );
// do something
...
if( SomeTest ) {
delete pObj;
return;
}
if( SomeOtherTest ) {
...
while( ... ) {
if( AThirdTest )
return;
}
}
// object no longer needed, delete it.
delete pObj;
}
So you see the big? Deep inside the while, there is an if which returns. It happened
to me, that I forgot to delete pObj, because I am human
An auto_ptr saves me from all of this. I don't need to remember to delete pObj, it
is done automatically for me. For the very same reason that you use vector instead
of dynamically allocated arrays, you use an auto_ptr when you must use a pointer, to
free yourself from human errors and to easen maintenance (who says that this test
was there in the first place. It could have been added weeks later).