I'm writing some code in MFC and I want to use auto pointers. I've come across two different classes that look like they do the same thing: CAutoPtr
and std::auto_ptr
What are people's thoughts about the two different implementations?
Further, I know there is std::tr1::shared_ptr
. Is there a similar shared_ptr
that is in ATL/MFC?
Both CAutoPtr
and auto_ptr
give you smart pointer semantics including transfer of ownership semantics. CAutoPtr
is an ATL class -- built using COM. It is a non-standard extension for a particular OS. auto_ptr
on the other hand is standard C++. If you want to use a container of such objects you have to use CAutoPtrArray
or CAutoPtrList
.
An important point to note is that there is something called auto_ptr_ref
that allows you to return auto_ptr
s as a return value. There is no such thing with CAutoPtr
.
auto_ptr
is deprecated in C++0x. Use unique_ptr
if you have to: you can use them in move-aware containers and also get some safety from unsafe implicit moves of l-values.