| title | CComPtr Class | Microsoft Docs | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ms.custom | ||||||||||||||
| ms.date | 11/04/2016 | |||||||||||||
| ms.reviewer | ||||||||||||||
| ms.suite | ||||||||||||||
| ms.technology |
|
|||||||||||||
| ms.tgt_pltfrm | ||||||||||||||
| ms.topic | reference | |||||||||||||
| f1_keywords |
|
|||||||||||||
| dev_langs |
|
|||||||||||||
| helpviewer_keywords |
|
|||||||||||||
| ms.assetid | 22d9ea8d-ed66-4c34-940f-141db11e83bd | |||||||||||||
| caps.latest.revision | 21 | |||||||||||||
| author | mikeblome | |||||||||||||
| ms.author | mblome | |||||||||||||
| manager | ghogen | |||||||||||||
| translation.priority.ht |
|
A smart pointer class for managing COM interface pointers.
template<class T>
class CComPtr
T
A COM interface specifying the type of pointer to be stored.
| Name | Description |
|---|---|
| CComPtr::CComPtr | The constructor. |
| Name | Description |
|---|---|
| CComPtr::operator = | Assigns a pointer to the member pointer. |
ATL uses CComPtr and CComQIPtr to manage COM interface pointers. Both are derived from CComPtrBase, and both perform automatic reference counting.
The CComPtr and CComQIPtr classes can help eliminate memory leaks by performing automatic reference counting. The following functions both perform the same logical operations; however, note how the second version may be less error-prone by using the CComPtr class:
[!code-cppNVC_ATL_Utilities#130]
[!code-cppNVC_ATL_Utilities#131]
In Debug builds, link atlsd.lib for code tracing.
CComPtr
Header: atlbase.h
The constructor.
CComPtr() throw ();
CComPtr(T* lp) throw ();
CComPtr (const CComPtr<T>& lp) throw ();
lp
Used to initialize the interface pointer.
T
A COM interface.
Assignment operator.
T* operator= (T* lp) throw ();
T* operator= (const CComPtr<T>& lp) throw ();
Returns a pointer to the updated CComPtr object
This operation AddRefs the new object and releases the existing object, if one exists.