| title | CComPtr Class | |||
|---|---|---|---|---|
| description | Reference guide to the Microsoft C++ Active Template Library (ATL) class CComPtr. | |||
| ms.date | 02/07/2020 | |||
| f1_keywords |
|
|||
| helpviewer_keywords |
|
|||
| ms.assetid | 22d9ea8d-ed66-4c34-940f-141db11e83bd |
A smart pointer class for managing COM interface pointers.
template<class T>
class CComPtrT
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 do automatic reference counting.
The CComPtr and CComQIPtr classes can help eliminate memory leaks by performing automatic reference counting. The following functions both do the same logical operations. However, the second version may be less error-prone because it uses 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.
The constructors that take an argument call AddRef on lp, if it isn't a null pointer. A non-null owned object gets a Release call upon the CComPtr object's destruction, or if a new object is assigned to the CComPtr object.
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.