This class is defined in Pointer.h. Because COM components use reference counting for lifetime management, it is important that you always release interfaces when you are done with them. Microsoft provides a template class that handles the lifetime automatically. For example, if you want a smart pointer for an XML interface, you could write something like this:
Pointer<IXMLDOMNode> pNewChild
pXmlDom->CreateNode(NODE_ELEMENT, L"MyElement", L"", &pNewChild);
The first line defines the smart pointer. The second line shows retrieving a smart pointer via another call. The & operator always releases an existing interface if it contains one and returns the address for the internal pointer. Once you have retrieved a pointer like this, the Pointer instance calls Release for you when the variable goes out of scope. Microsoft recommends that you use smart pointers instead of calling AddRef and Release manually.
In addition, the Pointer smart pointer class calls QueryInterface to retrieve other interfaces for you. For example, when the factory registry creates a new instance of a component, it has code like this:
PWizardComponent pComp = pUnknown;
if (pComp != nullptr)
pComp->SetContainer(m_pContainer);
The first line calls QueryInterface behind the scenes to request the IWizardComponent interface. The resulting smart pointer will equal nullptr if the component does not support that interface.
Related Topics