Excluding STL, I only found CComPtr in C++ windows programming. Is there any other types of smart pointers in windows SDK? Thanks.
-
CComPtr is an equivalent of STL's counted_ptr. Is there in windows api the equivalent of STL's copied_ptr, owned_ptr, linked_ptr, cow_ptr? – jiangok Apr 17 '11 at 01:28
-
2The C++ STL does not have anything called counter_ptr. It does have auto_ptr, which is not counted. – John Zwinck Apr 17 '11 at 01:29
-
1(stating the obvious) tr1, on the other hand, has a counted pointer named shared_ptr – Viktor Sehr Apr 18 '11 at 16:33
3 Answers
First, STL's and boost's smart pointers are available on Windows and there's nothing wrong with using those.
Speaking of purely Windows stuff, COM interface pointers, with their AddRef/Release lifetime management model, readily lends itself to smart pointers. There are some smart pointer classes in Windows-specific libraries that are geared towards storing COM interface pointers. In addition to the ATL's CComPtr<>, there's _com_ptr_t<> of Microsoft Native COM, and MFC's COleDispatchDriver. The latter is hardly ever used with the advent of Native COM. With the exception of CComPtr, those are used together with type library import facilities.

- 59,826
- 25
- 160
- 281
In the Windows SDK (specific to ATL), there is CAutoPtr(single item allocation) and CAutoVectorPtr (array allocation).

- 100,020
- 15
- 103
- 173
The MSDN article states that CComPtr is designed to be used with COM objects only. Generally Boost smart pointers are commonly used as a platform-independent C++ smart pointer library. Since the concept of smart pointers isn't bound to a particular OS, there's really no need to use a smart pointer implementation bound to Windows, even if that's the only platform you plan on developing the application for.

- 10,149
- 7
- 50
- 87
-
1If you are dealing with COM objects `CComPtr` makes sense. It uses the COM reference counting, so if you pass the pointer to other code not using the same smart pointer class, that callee can hold a reference. Plus you get the `QueryInterface` method built in. So in some circumstances it does make sense to use this, provided you're already writing highly non-portable code (say, a Windows GUI...) – asveikau Jun 02 '11 at 18:25