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– jiangok2011-04-17 01:28:21 +00:00Commented Apr 17, 2011 at 1:28
-
2The C++ STL does not have anything called counter_ptr. It does have auto_ptr, which is not counted.John Zwinck– John Zwinck2011-04-17 01:29:58 +00:00Commented Apr 17, 2011 at 1:29
-
1(stating the obvious) tr1, on the other hand, has a counted pointer named shared_ptrViktor Sehr– Viktor Sehr2011-04-18 16:33:52 +00:00Commented Apr 18, 2011 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.
Comments
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.
1 Comment
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...)