Skip to content

Latest commit

 

History

History
150 lines (128 loc) · 3.83 KB

File metadata and controls

150 lines (128 loc) · 3.83 KB
title ptr::operator= | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-windows
ms.tgt_pltfrm
ms.topic reference
f1_keywords
ptr.operator=
msclr.com.ptr.operator=
msclr::com::ptr::operator=
ptr::operator=
dev_langs
C++
helpviewer_keywords
operator=
ms.assetid 58619910-46c0-4db8-b183-c811b23b2df1
caps.latest.revision 8
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

ptr::operator=

Attaches a COM object to a com::ptr.

Syntax

ptr<_interface_type> % operator=(  
   _interface_type * _right  
);  

Parameters

_right
The COM interface pointer to attach.

Return Value

A tracking reference on the com::ptr.

Exceptions

If the com::ptr already owns a reference to a COM object, operator= throws xref:System.InvalidOperationException.

Remarks

Assigning a COM object to a com::ptr references the COM object but does not release the caller's reference to it.

This operator has the same effect as Attach.

Example

This example implements a CLR class that uses a com::ptr to wrap its private member IXMLDOMDocument object. The ReplaceDocument member function first calls Release on any previously owned object and then uses operator= to attach a new document object.

// comptr_op_assign.cpp  
// compile with: /clr /link msxml2.lib  
#include <msxml2.h>  
#include <msclr\com\ptr.h>  
  
#import <msxml3.dll> raw_interfaces_only  
  
using namespace System;  
using namespace System::Runtime::InteropServices;  
using namespace msclr;  
  
// a ref class that uses a com::ptr to contain an   
// IXMLDOMDocument object  
ref class XmlDocument {  
public:  
   // construct the internal com::ptr with a null interface  
   // and use CreateInstance to fill it  
   XmlDocument(String^ progid) {  
      m_ptrDoc.CreateInstance(progid);     
   }  
  
   // replace currently held COM object with another one  
   void ReplaceDocument(IXMLDOMDocument* pDoc) {  
      // release current document object  
      m_ptrDoc.Release();  
      // attach the new document object  
      m_ptrDoc = pDoc;  
   }  
  
   // note that the destructor will call the com::ptr destructor  
   // and automatically release the reference to the COM object  
  
private:  
   com::ptr<IXMLDOMDocument> m_ptrDoc;  
};  
  
// unmanaged function that creates a raw XML DOM Document object  
IXMLDOMDocument* CreateDocument() {  
   IXMLDOMDocument* pDoc = NULL;  
   Marshal::ThrowExceptionForHR(CoCreateInstance(CLSID_DOMDocument30, NULL,  
      CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pDoc));  
   return pDoc;  
}  
  
// use the ref class to handle an XML DOM Document object  
int main() {  
   IXMLDOMDocument* pDoc = NULL;  
  
   try {  
      // create the class from a progid string  
      XmlDocument doc("Msxml2.DOMDocument.3.0");  
  
      // get another document object from unmanaged function and  
      // store it in place of the one held by the ref class  
      pDoc = CreateDocument();  
      doc.ReplaceDocument(pDoc);  
      // no further need for raw object reference  
      pDoc->Release();  
      pDoc = NULL;  
   }  
   catch (Exception^ e) {  
      Console::WriteLine(e);     
   }  
   finally {  
      if (NULL != pDoc) {  
         pDoc->Release();  
      }  
   }  
}  

Requirements

Header file <msclr\com\ptr.h>

Namespace msclr::com

See Also

ptr Members
ptr::Attach
ptr::Detach
ptr::Release