views:

486

answers:

3

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?

+2  A: 

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_ptrs 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.

dirkgently
Nice answer. However, as far as I know CAutoPtr has nothing to do with COM.
Michael Burr
A: 

CAutoPtr is ATL specific .

std:auto_ptr and CAutoPtr both don't provide reference counting. It looks like both have same functionality.link text

I guess there is no shared_ptr in ATL/MFC. shared_ptr is implemented in boost library.

Alien01
+1  A: 

The closest thing to shared_ptr in ATL/MFC is CComPtr. It is meant to be used on COM objects, but it can be used on any class that includes AddRef/Release reference counting methods.

Mark Ransom