In our large project we have a lot class with the following typedef
's:
class Foo
{
public:
typedef std::auto_ptr<Foo> Ptr;
typedef boost::shared_ptr<Foo> Ref;
...
};
...
Foo::Ref foo(new Foo);
...
doBar(foo);
...
The using of them is very convenient. But I doubt if auto_ptr
is semantically close to Ptr
and shared_ptr
is the same as ref? Or should auto_ptr
be used explicitly since it has "ownership transfer" semantics?
Thanks,
std::auto_ptr
has ownership transfer semantics, but it's quite broken. If you can useboost::shared_ptr
, then you should useboost::unique_ptr
instead ofstd::auto_ptr
, since it does what one would expect. It transfers ownership and makes the previous instance invalid, whichstd::auto_ptr
doesn't.Even better, if you can use C++11, then swap to
std::unique_ptr
andstd::shared_ptr
.